[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.
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:
An article about what is Functional Programming (FP), and how does it differ from Object Oriented Programming (OOP) ?
Note that both these paradigms are used in R, and interact with each others.
Here’s the definition of what FP is :
- Programming == creating function
- Functions returns values computed from arguments (and only these)
- A function has no side effect