Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (7.3k points)

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?

1 Answer

0 votes
edited by

According to R Documentation:

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.


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.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 19, 2019 in R Programming by Ajinkya757 (5.3k points)
0 votes
1 answer

Browse Categories