Oh, where to begin? I’m a bit of an outspoken critic of the
operator. I’ve been a bit vocal about it within my department to the point
where I received the following acknowledgement:
This book came to exist after James noticed me writing a bunch of RMarkdown files as notes, and told me there was a better way,
bookdown. You can also blame him for the use of
=as the assignment operator.
— David Dalpiaz in the README file for Applied Statistics Textbook
And I’ve been known to lovingly question the rationale behind using
<- while contributing to projects in the R community:
A tale of
Within R there are 5 different assignment operators:
Though, at one point, there was a sixth assignment operator, the underscore
_, but that
was deprecated in R 1.9.0. The remaining assignment operators have some important
differences. In particular, the
->> operators lead to a dark side of
assignment to the global environment that will not be covered in this short dispatch.
Meanwhile, the main assignment operators given by
= are all
quite similar in that they perform an assignment operation.
The operator “=” is now allowed as an assignment operator in the grammar, for consistency with other languages, including recent versions of S-PLUS. Assignments with “=” are basically allowed only at top-level and in braced or parenthesized expressions, to make famous errors such as “if(x=0) 1 else 2” illegal in the grammar. (There is a plan to gradually eliminate the underscore as an assignment in future versions of R.)
The bold section was added to emphasize a pre-existing behavior of R that
= assignment wasn’t able to have: assignment in parentheses.
In particular, the affect of
-> operators change depending on where
they are called when compared to
=. Thus, they are said to have a greater
= operator. In particular, the change that occurs regarding
<- is in part a scope violation summarized aptly as:
The effect of
foo(x <- y)if the argument is evaluated is to change the value of
xin the calling environment and not in the evaluation environment of
That is to say, instead of assigning a temporary value to pass to the function,
<- operator overrides the calling location variable with a new assignment
value and then allows the function to be evaluated on the new value.
Take for example the following empty R session and the
that has parameter
x to receive data:
# Assignment with = mean(x = 1:5) ##  3 x ## Error: object 'x' not found # Assignment with <- mean(x <- 1:5) ##  3 x ##  1 2 3 4 5 # Remove x from the environment to allow # subsequent runs to obtain the first # error. rm(x)
Note: If you subsequently run this example, make sure to remove
from your environment!
So, we have the violation of scope that gets brought up. But, you may say like many of the folks I’ve discussed this with:
Why are you using
<-for assignment in a function parameter? Clearly that is the reason for why
To which I respond:
There should not be a need to differentiate between assignment operators in a programming language.
But, if scope violation isn’t your cup of tea, let’s construct three scenarios
that strikes at the heart of every programmer: readability. In each scenario,
we will present the
<- operator along side the
# Appropriate assignment a <- 42 a = 42 # Accidently writing less than a < -2 a = 2 # Readability diminishes... Is that -1? a<-1 a=1
In the end, the readibility of
However, the final argument I have against the de facto use of
<- is the fact
that it is four characters (2 spaces, 1 less than, and 1 minus sign) compared
to three characters (2 spaces and equal sign).
I realize that I may be in the minority on this topic. However, its important
to recognize that largely there have been no discussions as to why
<- is prefered outside of…
Darn kids, get off my lawn… Back in my day we would use
<-on our keyboard.
Or Google’s Style Guide that simply states:
=, for assignment.
x <- 5
x = 5
This style was also copied verbatim by
Hadley Wickham in Advanced R without
further explanation. In his most recent book,
R for Data Science,
he takes a more stronger view point and calls
= users lazy
without providing much of an explanation.
You will make lots of assignments and
<-is a pain to type. Don’t be lazy and use
=: it will work, but it will cause confusion later. Instead, use RStudio’s keyboard shortcut:
-(the minus sign).
— Hadley Wickham in R for Data Science: Coding Basics
Standard advice is to avoid using
=when you mean
<-even though it takes an extra keystroke. However, it is largely a matter of taste (as long as you know the differences).
— Patrick Burns in R Inferno
In short, assignment now is about style and preference. There is no
need to push assignment using
<- since modern keyboards lack the
=. Furthermore, by using the common assignment convention
throughout programming, the transference of skills from learning R to
other programming languages decreases.