[R] different behavior of $ with string literal vs string variable as argument

Ben Bolker bbolker at gmail.com
Sun Feb 10 22:58:59 CET 2013


Duncan Murdoch <murdoch.duncan <at> gmail.com> writes:

> 
> 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.

  [snip]

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

  I nominate this issue as a candidate for an R FAQ entry.  There's
at least one question on StackOverflow about it:

http://stackoverflow.com/questions/14123761/recursive-lists

I think I've seen more but I can't find them right now (it's a little
hard to search on "$" and "[[") ...



More information about the R-help mailing list