David Romano
2013-Feb-10 21:06 UTC
[R] different behavior of $ with string literal vs string variable as argument
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$nameNULL 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. David [[alternative HTML version deleted]]
R. Michael Weylandt
2013-Feb-10 21:31 UTC
[R] different behavior of $ with string literal vs string variable as argument
On Sun, Feb 10, 2013 at 9: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") >> 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? >You've already found it: the bracket operators.... MW> Thanks for your help in clarifying what might be going on here. > > David > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Duncan Murdoch
2013-Feb-10 21:40 UTC
[R] different behavior of $ with string literal vs string variable as argument
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
Bert Gunter
2013-Feb-10 21:59 UTC
[R] different behavior of $ with string literal vs string variable as argument
Please read the Help before posting. ?"$" says: "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. -- Bert 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") >> 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. > > David > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
David Romano
2013-Feb-10 22:16 UTC
[R] different behavior of $ with string literal vs string variable as argument
Sorry, this was meant to go to the full list. -David On Sun, Feb 10, 2013 at 2:15 PM, David Romano <dromano@stanford.edu> wrote:> > > On Sun, Feb 10, 2013 at 1:59 PM, Bert Gunter <gunter.berton@gene.com>wrote: > >> Please read the Help before posting. >> >> ?"$" says: >> > > It helps to know that $ must be quoted, so thanks again goes to Duncan for > pointing this out. > > >> "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. >> > > I did search, but as Ben points out in the next message in the thread, > it's tricky to formulate the search to get hits, and, for example, I > wouldn't have realized the post he refers to there involves the same issue > unless I already knew the answer. > > David > > -- Bert >> >> On Sun, Feb 10, 2013 at 1:06 PM, David Romano <dromano@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") >> >> 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. >> > >> > 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. >> >> >> >> -- >> >> Bert Gunter >> Genentech Nonclinical Biostatistics >> >> Internal Contact Info: >> Phone: 467-7374 >> Website: >> >> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm >> > >[[alternative HTML version deleted]]
Maybe Matching Threads
- using ifelse to remove NA's from specific columns of a data frame containing strings and numbers
- odd behavior of browser()
- how to "multiply" list of matrices by list of vectors
- how to parallelize 'apply' across multiple cores on a Mac
- using 'apply' to apply princomp to an array of datasets