Create variables by running a function
<- or = for assignments?
Both assignments work:
x <- 6 will create a variable called x with value 6
x = 4 will change the value of variable x to 4
When R was developed only <- was used for assignments and = was used for logical operations: is x equal to 4? It’s confusing to use the same symbol for assignment and logical operations. If you do that then
x = 4 could mean: x is 4 but also is x 4?
That is very messy.
Note that = was also used inside function calls to give values to arguments:
x <- mean(v, trim=0.1, na.rm=FALSE)
A lot of R users with programming experience hated the <- because all other programming languages use = for variable assignment. So R adopted both <- and = for assignment and started to use == for logical operations. They kept = inside function calls to give values to arguments since this is not confusing
x = 4
matrix(1:4,ncol=2,nrow=2)
The difference in meaning of = is clear here.
So now you can choose between <- and = for assignments. Old R programmers still prefer <- because it means you were already there in the beginning days of R and you’re cool while people who learned to program in another language before learning R prefer =