[R] Is there any difference between <- and =
"Jens Oehlschlägel"
oehl_list at gmx.de
Thu Mar 12 16:29:18 CET 2009
Sean,
> would like to receive expert opinion to avoid potential trouble
[..]
> i think the following is the most secure way if one really
> really has to do assignment in a function call
> f({a=3})
> and if one keeps this convention, <- can be dropped altogether.
secure is relative, since due to R's lazy evaluation you never know whether a function's argument is being evalutated, look at:
> f<- function(x)TRUE
> x <- 1
> f((x=2)) # obscured attempt to assign in a function call
[1] TRUE
> x
[1] 1
Thus there is dangerous advice in the referenced blog which reads:
"
f(x <- 3)
which means "assign 3 to x, and call f with the first argument set to the value 3
"
This might be the case in C but not in R. Actually in R "f(x <- 3)" means: call f with a first unevaluated argument "x <- 3", and if and only if f decides to evaluate its first argument, then the assignment is done. To make this very clear:
> f <- function(x)if(runif(1)>0.5) TRUE else x
> x <- 1
> print(f(x <- x + 1))
[1] TRUE
> print(f(x <- x + 1))
[1] 2
> print(f(x <- x + 1))
[1] 3
> print(f(x <- x + 1))
[1] TRUE
> print(f(x <- x + 1))
[1] 4
> print(f(x <- x + 1))
[1] 5
> print(f(x <- x + 1))
[1] TRUE
> print(f(x <- x + 1))
[1] 6
> print(f(x <- x + 1))
[1] TRUE
Here it is unpredictable whether your assignment takes place. Thus assigning like f({x=1}) or f((x=1))is the maximum dangerous thing to do: even if you have a code-reviewer and the guy is aware of the danger of f(x<-1) he will probably miss it because f((x=1)) does look too similar to a standard call f(x=1).
According to help("<-"), R's assignment operator is rather "<-" than "=":
"
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.
"
So my recommendation is
1) use R's assignment operator with two spaces around (or assign()) and don't obscure assignments by using C's assignment operator (or other languages equality operator)
2) do not assign in function arguments unless you have good reasons like in system.time(x <- something)
HTH
Jens Oehlschlägel
P.S. Disclaimer: you can consider me biased towards "<-", never trust experts, whether experienced or not.
P.P.S. a puzzle, following an old tradition:
What is going on here? (and what would you need to do to prove it?)
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics" "package:grDevices" "package:utils" "package:datasets" "package:methods"
[8] "Autoloads" "package:base"
> ls(all.names = TRUE)
[1] "y"
> y
[1] 1 2 3
> identical(y, 1:3)
[1] TRUE
> y[] <- 1 # assigning 1 fails
> y
[1] 1 2 3
> y[] <- 2 # assigning 2 works
> y
[1] 2 2 2
>
> # Tip: no standard packages modified, no extra packages loaded, neither classes nor methods defined, no print methods hiding anything, if you would investigate my R you would not find any false bottom anymore
>
> version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 2
minor 8.1
year 2008
month 12
day 22
svn rev 47281
language R
version.string R version 2.8.1 (2008-12-22)
--
More information about the R-help
mailing list