Live data from Hacker News

Snippets of code for things you do all the time in R

4dpiecharts.com

11–14 of 14 posts

Re: Snippets of code for things you do all the time in R

#11

Earlier quoted context omitted.

I am of the opposite opinion, but perhaps I can be converted. Do you have an example of where it would be required to use '<-'? Not just median(x <- 10), but a use in which there no simple '=' alternative.

I've added some examples to my above comment. My example is perhaps a little closer to 'median(x <- 10)' than you were asking for, but I think it illustrates the point that using '=' will not always have the desired result in places where '<-' would.

My problem is that between work in work and work in startup I need to write in about 8 languages.

This applies in all languages:

vec1 = rnorm(100)

system.time(vec1)

mean(vec1)

And, I can get my colleagues to fix it when I screw up, even though they are not R-heads.

Re: Snippets of code for things you do all the time in R

#12
post #9

Clearly more a comment for an R forum, but why the hell does anybody bother using '<-' instead of '='? Are you trying to obfuscate?

My understanding has always been that <- is the way that S did it, so that's the way R originally did it but = is almost always equivalent. I haven't found a place in my modest R hacking yet that = hasn't worked the way I expected it to...

And I haven't found anything in my ten years of using R in statistical analysis where '=' didn't work either. I've written a lot of R code. A hedge process on €2bn is being monitored with '=' signs as we speak. Dastardly.

Re: Snippets of code for things you do all the time in R

#13

Earlier quoted context omitted.

I've added some examples to my above comment. My example is perhaps a little closer to 'median(x <- 10)' than you were asking for, but I think it illustrates the point that using '=' will not always have the desired result in places where '<-' would.

My problem is that between work in work and work in startup I need to write in about 8 languages. This applies in all languages: vec1 = rnorm(100) system.time(vec1) mean(vec1) And, I can get my colleagues to fix it when I screw up, even though they are not R-heads.

You gotta do what you gotta do, but your example probably doesn't do what you want in R.

  > system.time(vec1 
My computer takes .2 seconds to generate the million random normals and assign them to vec1.

  > vec1 = rnorm(1000000)
  > system.time(vec1)
     user  system elapsed 
        0       0       0 
 
My computer only takes 0 seconds to evaluate the existing vec1 of length one million.

Re: Snippets of code for things you do all the time in R

#14

Earlier quoted context omitted.

My problem is that between work in work and work in startup I need to write in about 8 languages. This applies in all languages: vec1 = rnorm(100) system.time(vec1) mean(vec1) And, I can get my colleagues to fix it when I screw up, even though they are not R-heads.

You gotta do what you gotta do, but your example probably doesn't do what you want in R. > system.time(vec1 My computer takes .2 seconds to generate the million random normals and assign them to vec1. > vec1 = rnorm(1000000) > system.time(vec1) user system elapsed 0 0 0 My computer only takes 0 seconds to evaluate the existing vec1 of length one million.

Ah - sorry - I was being a bit t'ick, and didn't twig the time thing. Should I require it, I will keep it in mind. Thank you.
Post reply on HN