What are the differences between the assignment operators =
and <-
in R?
I know that operators are slightly different, as this example shows
x <- y <- 5
x = y = 5
x = y <- 5
x <- y = 5
# Error in (x <- y) = 5 : could not find function "<-<-"
But is this the only difference?
The difference in assignment operators is clearer when you use them to set an argument value in a function call. For example:
In this case,
x
is declared within the scope of the function, so it does not exist in the user workspace.In this case,
x
is declared in the user workspace, so you can use it after the function call has been completed.There is a general preference among the R community for using
<-
for assignment (other than in function signatures) for compatibility with (very) old versions of S-Plus. Note that the spaces help to clarify situations likeMost R IDEs have keyboard shortcuts to make
<-
easier to type. Ctrl + = in Architect, Alt + - in RStudio (Option + - under macOS), Shift + - (underscore) in emacs+ESS.If you prefer writing
=
to<-
but want to use the more common assignment symbol for publicly released code (on CRAN, for example), then you can use one of thetidy_*
functions in theformatR
package to automatically replace=
with<-
.The answer to the question "Why does
x <- y = 5
throw an error but notx <- y <- 5
?" is "It's down to the magic contained in the parser". R's syntax contains many ambiguous cases that have to be resolved one way or another. The parser chooses to resolve the bits of the expression in different orders depending on whether=
or<-
was used.To understand what is happening, you need to know that assignment silently returns the value that was assigned. You can see that more clearly by explicitly printing, for example
print(x <- 2 + 3)
.Secondly, it's clearer if we use prefix notation for assignment. So
The parser interprets
x <- y <- 5
asWe might expect that
x <- y = 5
would then bebut actually it gets interpreted as
This is because
=
is lower precedence than<-
, as shown on the?Syntax
help page.As your example shows,
=
and<-
have slightly different operator precedence (which determines the order of evaluation when they are mixed in the same expression). In fact,?Syntax
in R gives the following operator precedence table, from highest to lowest:But is this the only difference?
Since you were asking about the assignment operators: yes, that is the only difference. However, you would be forgiven for believing otherwise. Even the R documentation of
?assignOps
claims that there are more differences:Let’s not put too fine a point on it: the R documentation is (subtly) wrong [1]. This is easy to show: we just need to find a counter-example of the
=
operator that isn’t (a) at the top level, nor (b) a subexpression in a braced list of expressions (i.e.{…; …}
). — Without further ado:Clearly we’ve performed an assignment, using
=
, outside of contexts (a) and (b). So, why has the documentation of a core R language feature been wrong for decades?It’s because in R’s syntax the symbol
=
has two distinct meanings that get routinely conflated:=
operator it performs no action at runtime, it merely changes the way an expression is parsed.Let’s see.
In any piece of code of the general form …
… the
=
is the token that defines named argument passing: it is not the assignment operator. Furthermore,=
is entirely forbidden in some syntactic contexts:Any of these will raise an error “unexpected '=' in ‹bla›”.
In any other context,
=
refers to the assignment operator call. In particular, merely putting parentheses around the subexpression makes any of the above (a) valid, and (b) an assignment. For instance, the following performs assignment:But also:
Now you might object that such code is atrocious (and you may be right). But I took this code from the
base::file.copy
function (replacing<-
with=
) — it’s a pervasive pattern in much of the core R codebase.The original explanation by John Chambers, which the the R documentation is probably based on, actually explains this correctly:
A confession: I lied earlier. There is one additional difference between the
=
and<-
operators: they call distinct functions. By default these functions do the same thing but you can override either of them separately to change the behaviour. By contrast,<-
and->
(left-to-right assignment), though syntactically distinct, always call the same function. Overriding one also overrides the other. Knowing this is rarely practical but it can be used for some fun shenanigans.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.Google's R style guide simplifies the issue by prohibiting the "=" for assignment. Not a bad choice.
https://google.github.io/styleguide/Rguide.xml
The R manual goes into nice detail on all 5 assignment operators.
http://stat.ethz.ch/R-manual/R-patched/library/base/html/assignOps.html
This may also add to understanding of the difference between those two operators:
For the first element R has assigned values and proper name, while the name of the second element looks a bit strange.
R version 3.3.2 (2016-10-31); macOS Sierra 10.12.1
According to John Chambers, the operator
=
is only allowed at "the top level," which means it is not allowed in control structures likeif
, making the following programming error illegal.As he writes, "Disallowing the new assignment form [=] in control expressions avoids programming errors (such as the example above) that are more likely with the equal operator than with other S assignments."
You can manage to do this if it's "isolated from surrounding logical structure, by braces or an extra pair of parentheses," so
if ((x = 0)) 1 else x
would work.See http://developer.r-project.org/equalAssign.html