Profile picture
Colin Fay @_ColinFay
, 16 tweets, 7 min read Read on Twitter
[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 :
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:
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Colin Fay
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member and get exclusive features!

Premium member ($3.00/month or $30.00/year)

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!