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

David Winsemius dwinsemius at comcast.net
Thu Aug 27 06:01:24 CEST 2009


On Aug 26, 2009, at 11:35 PM, Ning Ma wrote:

> 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()

What do you really want to achieve or to learn? Pretty much anything  
ending in () will be seen as a function and it does not appear that  
you have a function inside L.

 > L <-list()
 > L$aaa <- 5753
 > L$aaa <- "5753"
 > L
$aaa
[1] "5753"

Note that aaa, which is a valid object name, does not have surrounding  
back-quotes as in your question. And even if you created an aaa  
portion of L using backquotes they would not be displayed that way:

 > L <-list()
 > L$`aaa` <- "5753"
 > L
$aaa
[1] "5753"

If you constructed an invalid object name within back-quotes you might  
see them later:

 > L <-list()
 > L$`5753` <- "5753"
 > L
$`5753`
[1] "5753"

So what is your real question and what does str(L) report?

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list