x <- 1:10
y = 1:10
identical(x, y)
# TRUE
Question: The effect of = seems equivalent to <-. I’m already used to =, should I change my habits for R language?
I prefer <-. Let me tell you my reasons.
-
<<-, which looks more like<-, is needed when assigning values to the parent environment.
This is probably the most important reason. People will need this functionality sometime. For the demonstration of using<<-, please refer to my lexical scoping article. -
<-has higher precedence than=does in R syntax.
Why doesn’t the final case work in the following lines? The reason is related to the parser of R. This thread on Stack Overflow provides more detailed elaboration. Intuitively, the statementa <- b = 15requiresb, which is likely to be unknown, to be assigned toafirst. However, given that b may not have a value, R has to processb = 15first, which is contradictory to R grammar.a <- b <- 5 a; b # 5 # 5 a = b <- 10 a; b # 10 # 10 a <- b = 15 # Error in a <- b = 15: could not find function "<-<-" -
In most cases of assigning values,
=is equivalent to<-, but<-is even more general than=.
In the first following case, x exists only in thesumfunction scope, while in the second case, x is also declared in Global Environment. Although we don’t often need this kind of declaration. Still, using=when passing arguments and<-when assigning values, as well as proper indents, can make your codes look more compilable. Consider the examples below:sum(x = c(5, 8, 7)) # 20 x # Error: object 'x' not found sum(x <- c(5, 8, 7)) # 20 x # 5 8 7
To sum up, using = or <- really depends on your preference. You can surely use = all the time, but I have given my personal suggestions.