128

I'm using R 2.8.1 and it is possible to use both = and <- as variable assignment operators. What's the difference between them? Which one should I use?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Mehper C. Palavuzlar
  • 10,089
  • 23
  • 56
  • 69

2 Answers2

69

From here:

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.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 7
    As `<-` can be used anywhere, does this mean that there is no need to use `=` ? – Mehper C. Palavuzlar Feb 16 '10 at 09:00
  • 31
    No, you need to still need to use `=` when calling functions to avoid assigning globally. Look at these examples: http://www.mayin.org/ajayshah/KB/R/html/b1.html. If you used `name<-"paypal", x<-2, ...` it would set `x` at the top level. Try running that example but writing `<-` instead of `=` and see what happens. – Mark Byers Feb 16 '10 at 09:07
  • 5
    The documentation is (still to this day) wrong. `=` is *not* only allowed on the top level. Except for operator precedence, the `<-` and `=` assignment operators are completely identical by default. R complicates matters by giving `=` a *secondary* syntactic meaning, besides its use as an assignment operator. – Konrad Rudolph Feb 13 '20 at 10:37
18

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)

A misleading feature of the assignment operator <- is found in Boolean expressions such as

> if (x[1]<-2) ...

which is supposed to test whether or not x[1] is less than -2 but ends up allocating 2 to x[1], erasing its current value! Note also that using

> if (x[1]=-2) ...

mistakenly instead of (x[1]==-2) has the same consequence."

gd047
  • 29,749
  • 18
  • 107
  • 146
  • I also think that `global assignment`, whatever the need for doing something like this, can only be performed using the ("gets"-based) `<<-` operator, and that there is not an "equals"-based equivalent. – gd047 Feb 16 '10 at 14:59
  • 10
    Google's R style guide says the opposite: http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html#assignment – Mark Byers Aug 08 '10 at 09:52
  • 1
    `if (x[1]=-2)` is conveniently prohibited to prevent this kind of error. Specifically, the operator `=` is only allowed at the top level. – Aaron left Stack Overflow Mar 14 '11 at 13:56
  • 4
    He asked what the difference was. This is a non-answer. – aaa90210 Dec 02 '14 at 03:24
  • 1
    Reading the official R documentation, I'm not sure this statement is true: *An older assignment operator is <- and, for compatibility reasons, it still remains functional,* – moo Apr 15 '22 at 05:53