[R] assigment operator question

Henrik Bengtsson hb at biostat.ucsf.edu
Sat Apr 7 22:51:46 CEST 2012


On Sat, Apr 7, 2012 at 1:30 PM, Mark Heckmann <mark.heckmann at gmx.de> wrote:
> Hello,
>
> using the <<- assignment operator I do not understand why the following does not work.
>
> l <<- list()
> l
> list()
> l$arg1 <<- "test"
> error in l$arg1 <<- "test" : Objekt 'l' not found
>
> ?"<<-" says:  "The operators <<- and ->> cause a search to made through the environment for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment. "
>
> Still I do noch understand why the above does not work. The object l is in the global environment. Can someone explain it to me?

Yes, the object 'l' is in the global environment, but 'l$arg1' is not,
cf. exists("l$arg1") and exists("l").  Instead, this works:

l <<- list();
l$args1 <- "test";

but much much much much much much much much much much much much much
much much much much much much much much much much much much much much
much much much much much much much much much much much better, do NOT
use "<<-" (or assign()/get()) unless you fully understand what you're
doing and have a very good reason for doing it, and that reason should
be able pass the embarrassment-test on public R mailing lists.  Got
the point?  Don't use it - there is another way to do what you want to
achieve - you just have to ask/find out how.

/H

>
> Thanks
> --Mark
> ––––––––––––––––––––––––––––––––––––
> Mark Heckmann
> Blog: www.markheckmann.de
> R-Blog: http://ryouready.wordpress.com
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list