Comment

Comment by ltjohnson

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.

parent ltjohnson 16 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton