[Thread]
Why do we use arrow `<-` instead of equal `=` for assignment in #RStats :
It’s an historical choice: R comes from S, which used <- for assignment. S uses `<-` partly because it is inspired by APL, which had the ← operator for assignment, as it was developed for this keyboard, which has a key for arrow :
en.wikipedia.org/wiki/APL_(prog…
Not that at that time, with APL, the arrow was chosen because it distinguished from the equal operator (there were no `==` for testing equality).
See : softwarepreservation.org/projects/apl/B…
Until 2001, in R, `=` could only be used for assigning function arguments, like `fun(foo = "bar")` (remember that R was born in 1993).
So before 2001, the `<-` was the standard (and only way) to assign value into a variable : developer.r-project.org/equalAssign.ht…
Historical fun fact: in the beginning of R, the `_` was used as an assignment operator. It was deprecated in R 1 :
cran.r-project.org/src/base/NEWS.1
`=` was mainly added because other languages uses `=` as an assignment method, and because it increased compatibility with S-Plus. Nowadays, there are seldom any cases when you can’t use one in place of the other. It’s safe to use `=` almost everywhere.
Yet, `<-` is preferred and adviced in R Coding style guides:
google.github.io/styleguide/Rgu…
adv-r.had.co.nz/Style.html
One reason, if not historical, to prefer the `<-` is that it clearly states in which side you are making the assignment (you can assign from left to right or from right to left in R):
The RHS assignment can for example be used for assigning the result of a pipe:
rud.is/b/2015/02/04/a…
There are some environment and precedence differences. For example, assignment with `=` is only done on a functional level, whereas `<-` does it on the top level when called inside the function() (but don’t do that):
There is also a difference in parsing when it comes to both these operators (but I guess this never happens in the real world), one failing and not the other:
It is also good practice because it clearly indicates the difference between function arguments and assignation:
Again, on environment assignment :
Some resources used here:
- blog.revolutionanalytics.com/2008/12/use-eq… by @revodavid
- stackoverflow.com/a/1742550/8236… by @richierocks
- stat.ethz.ch/R-manual/R-dev…
- ironholds.org/projects/rbitr…
Also, here's an example of a little comparison of using `=` and `<-` to assign the result of an equality test:
Oh, and a little bit unrelated, but I almost forgot this one:
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.