Since R3.0, a for
loop returns NULL
(as documented):
x <- for(i in 1:3) {i+1}
x
# NULL
However, using the right-arrow assignment, it seems to returns the last value:
for(i in 1:3) {i+1} -> x
x
# [1] 4
The documentation doesn't comment on ->
, and as pointed by Ben Bolker, the ->
seems to be converted to <-
anyway:
expression(1 -> x)
# expression(x <- 1)
So my questions are:
- Is this a bug or a misunderstanding?
- Why would
->
behave differently from<-
in that case, if they're supposed to be identical?