[R] different behavior of $ with string literal vs string variable as argument
gunter.berton at gene.com
Sun Feb 10 22:59:35 CET 2013
Please read the Help before posting.
"Both [[ and $ select a single element of the list. The main
difference is that $ **does not allow computed indices** , whereas [[
does. x$name is equivalent to x[["name", exact = FALSE]]. Also, the
partial matching behavior of [[ can be controlled using the exact
argument. " [emphasis added]
In other words, $ does not evaluate its argument.
This also appeared just a couple of days ago on this list, so please
also search Help archives before posting.
On Sun, Feb 10, 2013 at 1:06 PM, David Romano <dromano at stanford.edu> 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")
>  "abc"
>  "abc"
>> name <- "ss"
> 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
> Thanks for your help in clarifying what might be going on here.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Genentech Nonclinical Biostatistics
Internal Contact Info:
More information about the R-help