News for hackers. Stuff that matters.
Re:Snippets of code for things you do all the time in R by revorad on Tuesday January 11, 2011 @09:35AM (#2091954)
shameless plug: My book R Graph Cookbook is out soon - https://www.packtpub.com/r-graph-cookbook/book. It contains recipes for doing things with graphs, which can sometimes be obscure, annoying and time consuming.
Re:Snippets of code for things you do all the time in R by john_horton on Tuesday January 11, 2011 @09:55AM (#2092028)
Interesting - do you have a sample chapter you can make public?

(if there already is on the site you linked to, sorry for being obtuse).

Re:Snippets of code for things you do all the time in R by revorad on Tuesday January 11, 2011 @10:10AM (#2092071)
Sample chapter coming up shortly. If you don't see it on the linked page in a couple of days, please email me (in profile).
Re:Snippets of code for things you do all the time in R by hessenwolf on Tuesday January 11, 2011 @10:40AM (#2092161)
This would be nice an format with short functions that could be upvoted (like hacker news comments). Three shortcuts is a rather limited start.
Re:Snippets of code for things you do all the time in R by hessenwolf on Tuesday January 11, 2011 @10:41AM (#2092171)
Clearly more a comment for an R forum, but why the hell does anybody bother using '<-' instead of '='? Are you trying to obfuscate?
Re:Snippets of code for things you do all the time in R by ltjohnson on Tuesday January 11, 2011 @11:08AM (#2092265)
Using '<-' is the general style of the R community. The accepted (but not universal) practice is to use '<-' for assignment and '=' in argument passing. This is a good idea because '<-' and '=' are not equivalent and '<-' is more likely the operator you want. From the help page [1] The operator ‘<-’ can be used anywhere, whereas the operator ‘=’ is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions. That being said, some R community members still use '=' for assignment, but it's frowned upon. It certainly annoys me when I see it in R code.

Simple example to show when '=' won't work but '<-' will. Pilfered from "R Inferno" [2], which is an excellent reference for R idioms and best practices.

  system.time(vec1 <- rnorm(1000))
  mean(vec1)
  system.time(vec2 = rnorm(1000))
  mean(vec2)
And to demonstrate that you need to use '=' for argument passing:

  x <- c(3,4,NA)
  mean(x)
  mean(x, na.rm=TRUE)
  mean(x, na.rm<-TRUE)
[1] Type help('=') at an R prompt, or view it online: http://stat.ethz.ch/R-manual/R-patched/library/base/html/ass...

[2] http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

edit: clarified statement.

edit: added examples.

Re:Snippets of code for things you do all the time in R by hessenwolf on Tuesday January 11, 2011 @12:04PM (#2092561)
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.

Re:Snippets of code for things you do all the time in R by ltjohnson on Tuesday January 11, 2011 @12:38PM (#2092723)
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.
Re:Snippets of code for things you do all the time in R by hessenwolf on Thursday January 13, 2011 @07:19AM (#2099569)
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 by ltjohnson on Thursday January 13, 2011 @11:58AM (#2100417)
You gotta do what you gotta do, but your example probably doesn't do what you want in R.

  > system.time(vec1 <- rnorm(1000000))
     user  system elapsed 
    0.200   0.000   0.199 
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 by hessenwolf on Thursday January 13, 2011 @12:19PM (#2100510)
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.
Re:Snippets of code for things you do all the time in R by phuff on Tuesday January 11, 2011 @12:33PM (#2092698)
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...
Re:Snippets of code for things you do all the time in R by hessenwolf on Thursday January 13, 2011 @08:37AM (#2099698)
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.