I was wondering if there is a technical difference between the assignment operators "=" and "<-" in R. So, does it make any difference if I use:
Example 1: a = 1 or a <- 1
Example 2: a = c(1:20) or a <- c(1:20)
Thanks for your help
Sven
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes there is. This is what the help page of
'='says:With “can be used” the help file means assigning an object here. In a function call you can’t assign an object with
=because=means assigning arguments there.Basically, if you use
<-then you assign a variable that you will be able to use in your current environment. For example, consider:This just makes a 2 row matrix. Now consider:
This also gives you a two row matrix, but now we also have an object called
nrowwhich evaluates to 2! What happened is that in the second use we didn’t assign the argumentnrow2, we assigned an objectnrow2 and send that to the second argument ofmatrix, which happens to be nrow.Edit:
As for the edited questions. Both are the same. The use of
=or<-can cause a lot of discussion as to which one is best. Many style guides advocate<-and I agree with that, but do keep spaces around<-assignments or they can become quite hard to interpret. If you don’t use spaces (you should, except on twitter), I prefer=, and never use->!But really it doesn’t matter what you use as long as you are consistent in your choice. Using
=on one line and<-on the next results in very ugly code.