views:

146

answers:

1
+5  A: 

No, they are not exactly the same: the = operator cannot be used everywhere that <- can.

The operators <- and = assign into the environment in which they are evaluated. 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.

There are also differences in scope. See this answer for more details.

Which is better depends on who you ask.


Reading from "Introducing Monte Carlo Methods with R", by Robert and Casella:

"The assignment operator is =, not to be confused with ==, which is the Boolean operator for equality. An older assignment operator is <- and, for compatibility reasons, it still remains functional, but it should be ignored to ensure cleaner programming. (As pointed out by Spector, P. (2009). 'Data Manipulation with R' - Section 8.7., an exception is when using system.time, since = is then used to identify keywords)

Source


On the other hand, Google's R style guide recommends using <-:

Assignment

Use <-, not =, for assignment.

GOOD:
x <- 5

BAD:
x = 5

Mark Byers
That's a bizarre statement from P. Spector, no? I feel like the prevailing "idiomatic" style is still to use "<-" for assignment and "=" for arg lists, esp. from r-help and bioc mailing lists, no?
Steve Lianoglou