Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment operators in R: '<-' and '<<-'

What are the differences in the assignment operators <- and <<- in R?

And when should <<- be used?

like image 866
csiu Avatar asked Mar 21 '23 12:03

csiu


1 Answers

<- assigns an object to the environment in which it is evaluated (local scope). <<- assigns an object to the next highest environment that the name is found in or the global namespace if no name is found. See the documentation here.

<<- is usually only used in functions, but be careful. <<- can be much harder to debug because it is harder to trace the evaluation of the assignment. It is better to write functions with return statements instead.

Hadley Wickham has a good explination in his Advanced R Programming Book.

like image 76
Christopher Louden Avatar answered Apr 06 '23 06:04

Christopher Louden