What's the difference between `=` and `<-` in R?
I'm using R 2.8.1 and it is possible to use both = and <- as variable assignment operators. What's the difference between them? Which one should I use?
Top keyword - difference between = and <- in r
According to R Documentation, difference between = and <- in r:
The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.
The operators <<- and ->> are normally only used in functions, and cause a search to be made through parent environments for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise, the assignment takes place in the global environment.
Examples
mean(x = 1:10) x ## Error: object 'x' not found
In this case, x is declared within the scope of the function, so it does not exist in the user workspace.
mean(x <- 1:10) x ## [1] 1 2 3 4 5 6 7 8 9 10
In this case, x is declared in the user workspace, so you can use it after the function call has been completed.
To read more about assignment operators check this link.