On Sun, Feb 10, 2013 at 1:40 PM, Duncan Murdoch <murdoch.dun...@gmail.com>wrote:

> On 13-02-10 4:06 PM, David Romano wrote:
>
>> Hi everyone,
>>
>> I ran into the issue below while trying to execute a command of the form
>>
>> apply(list.names,1, function(x)  F(favorite.list$x) )
>>
>> where list.names is a character vector containing the names of the
>> elements
>> of favorite.list and F is some function defined on a list element.
>>
>> Namely,  the $ operator doesn't treat the string variable 'x' as the
>> string
>> it represents, so that, e.g.
>>
>>  ll <- list(ss="abc")
>>> ll$ss
>>>
>> [1] "abc"
>>
>>> ll$"ss"
>>>
>> [1] "abc"
>>
>> but
>>
>>  name <- "ss"
>>> ll$name
>>>
>> NULL
>>
>> I can get around this by using integers and the [[ and [ operators, but
>> I'd
>> like to be able to use names directly, too -- how would I go about doing
>> this?
>>
>> Thanks for your help in clarifying what might be going on here.
>>
>
> You can use names with [[, e.g.
>
> ll[[name]]
>
> will work in your example.  You can see more details in the help topic
> help("$"), in the section "Recursive (list-like) objects".
>
> Duncan Murdoch
>
>

Thanks, Duncan (and Michael, earlier); this clear everything up.  And just
so the help topic language is included in this thread:
------------
Recursive (list-like) objects

Indexing by [ is similar to atomic vectors and selects a list of the
specified element(s).

Both [[ and $ select a single element of the list. The main difference is
that $ does not allow computed indices, whereas [[ does.
-------------

which I take to mean that the argument to $ cannot require evaluation of
any kind, and so must be a string literal.

Thanks again,
David

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to