[R] How to refer to a list member by variable

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Dec 1 20:15:47 CET 2008


On Mon, 2008-12-01 at 10:53 -0800, Reitsma, Rene - COB wrote:
> Dear All,
> 
> I'm hoping one of you can help me with the following R problem. I'm
> trying to refer to a member of a list by variable. However, this seems
> not to work:
> 
> foo=list(first=c(1:10),second=c(11:20))
> 
> > foo$first
>  [1]  1  2  3  4  5  6  7  8  9 10
> 
> > foo$"first"
>  [1]  1  2  3  4  5  6  7  8  9 10
> 
> > goo=c("first")
> 
> > foo$goo
> NULL

foo[[goo]]

See ?`[[` for more details. Not sure if there is an incantation that
will work with '$' in this case.

HTH

G

> 
> > foo$goo[1]
> NULL
> 
> I'm sure I'm missing something quite elementary. Perhaps you can help?
> 
> RR
> 
> ______________________________________________
> 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