[R] is it safe to replace every "<-" by "=" in R code?

Mark Wardle mark at wardle.org
Mon Jan 14 08:12:32 CET 2008


On 13/01/2008, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> No.
>
> > f <- function(a = 3, b = 4) a-b
> > f(b = 10)
> [1] -7
> > f(b <- 10)
> [1] 6
>

I had to go and read (and re-read) the R manual on lexical scope to
try and understand this since this example highlights a previously
neglected "chasm" in my knowledge base!

Why does that give the result it does?

I can see that
f(b <- 10) is equivalent to   f(assign("b"), 10))

I can't work out whether that is interpreted within the parent
environment or the function's own environment at the time of call. Why
does R give the answer it does? This can't be a scope issue?  I would
have expected either -1 or -7!

Am I being completely dim?

-- 
Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK




More information about the R-help mailing list