[R] how to index a list with a string?

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Aug 29 18:03:19 CEST 2009



Peter Alspach wrote:
> Tena koe
> 
> Try either
> L[foo()]
> or
> L[[foo()]]
> 
> These return subtly (or not so subtly depending on your point of view)
> different results.

which is quite important, hence read the documentation. [] returns a 
list of length 1 (or a vector of length 1 of type list) where [[]] 
returns the value of the corresponding list element.
This also means that mutpiple indices are interpreted recursively in 
[[]] but will select the corresponding list elements in [].


Best,
Uwe Ligges



> HTH ....
> 
> Peter Alspach
> 
>> -----Original Message-----
>> From: r-help-bounces at r-project.org 
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Ning Ma
>> Sent: Thursday, 27 August 2009 3:35 p.m.
>> To: r-help at r-project.org
>> Subject: [R] how to index a list with a string?
>>
>> Hi, everybody. I have a list obj L, sth like
>>
>> $`aaa`
>> [1] "5753"
>>
>> if the string 'aaa' is a returned value of a function foo(). 
>> what is the right syntax form of L$foo()
>>
>> I'm new to R, thanks in advance.
>>
>> ______________________________________________
>> 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.
>>
> 
> ______________________________________________
> 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