[R] how to make this get command work?

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Thu Dec 18 06:14:30 CET 2014


One of the reasons lists are useful is that you can put various things in them and then you have an object name that you can hard code into your program, yet still use variables to find objects in that list. That is you do not need to directly use the get function at all.

foo[[var]]

If you ever think this is not true, just make a new list and put your old list into it, and you can start using variables to look up your old list.

lname <- "foo"
bar <- list( foo= list(A = c(1,3), B =c(1, 2), C = c(3, 1)) )
bar[[lname]][[var]]
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On December 17, 2014 7:24:56 PM PST, ce <zadig_1 at excite.com> wrote:
>Dear all,
>
>If I have a list like this how I can get an object of it  with a
>variable :
>
>foo<-list(A = c(1,3), B =c(1, 2), C = c(3, 1))
>var <- "A"
>
>get(paste("foo$",'A',sep=''))
>Error in get(paste("foo$", "A", sep = "")) : object 'foo$A' not found
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.



More information about the R-help mailing list