This might be an trivial thing but I am stuck. Consider: xx <- list(a=1:5, b=letters[1:5]) Although object xx is accessible through its name, how can object xx$b be accessed similarly through its name?> get("xx")$a [1] 1 2 3 4 5 $b [1] "a" "b" "c" "d" "e"> get("xx$b")Error in get(x, envir, mode, inherits) : variable "xx$b" was not found get("xx")$b will not work in my case because it will probably require parsing to make it work within a function. E.g. my.length <- function(...) { names <- as.character(substitute(list(...)))[-1] sapply(names, FUN=function(x){y <- get(x); length(y)}) }> my.length(xx)xx 2> my.length(xx$a)Error in get(x, envir, mode, inherits) : variable "xx$a" was not found> my.length(xx$a, xx$b)Error in get(x, envir, mode, inherits) : variable "xx$a" was not found Thank you. Christos Hatzis, Ph.D. Nuvera Biosciences, Inc. 400 West Cummings Park Suite 5350 Woburn, MA 01801 Tel: 781-938-3830 www.nuverabio.com
use 'eval' and 'parse'> xx$a [1] 1 2 3 4 5 $b [1] "a" "b" "c" "d" "e"> eval(parse(text='xx$a'))[1] 1 2 3 4 5>So that way you can pass in the character string and then 'parse' it. On 12/24/06, Christos Hatzis <christos@nuverabio.com> wrote:> > This might be an trivial thing but I am stuck. > > Consider: > > xx <- list(a=1:5, b=letters[1:5]) > > Although object xx is accessible through its name, > how can object xx$b be accessed similarly through its name? > > > get("xx") > $a > [1] 1 2 3 4 5 > > $b > [1] "a" "b" "c" "d" "e" > > > get("xx$b") > Error in get(x, envir, mode, inherits) : variable "xx$b" was not found > > get("xx")$b will not work in my case because it will probably require > parsing to make it work within a function. E.g. > > my.length <- function(...) { > names <- as.character(substitute(list(...)))[-1] > sapply(names, FUN=function(x){y <- get(x); length(y)}) > } > > my.length(xx) > xx > 2 > > my.length(xx$a) > Error in get(x, envir, mode, inherits) : variable "xx$a" was not found > > my.length(xx$a, xx$b) > Error in get(x, envir, mode, inherits) : variable "xx$a" was not found > > Thank you. > > Christos Hatzis, Ph.D. > Nuvera Biosciences, Inc. > 400 West Cummings Park > Suite 5350 > Woburn, MA 01801 > Tel: 781-938-3830 > www.nuverabio.com > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
On 12/24/2006 12:06 AM, Christos Hatzis wrote:> This might be an trivial thing but I am stuck. > > Consider: > > xx <- list(a=1:5, b=letters[1:5]) > > Although object xx is accessible through its name, > how can object xx$b be accessed similarly through its name?name <- "b" xx[[name]] will work. Duncan Murdoch> >> get("xx") > $a > [1] 1 2 3 4 5 > > $b > [1] "a" "b" "c" "d" "e" > >> get("xx$b") > Error in get(x, envir, mode, inherits) : variable "xx$b" was not found > > get("xx")$b will not work in my case because it will probably require > parsing to make it work within a function. E.g. > > my.length <- function(...) { > names <- as.character(substitute(list(...)))[-1] > sapply(names, FUN=function(x){y <- get(x); length(y)}) > } >> my.length(xx) > xx > 2 >> my.length(xx$a) > Error in get(x, envir, mode, inherits) : variable "xx$a" was not found >> my.length(xx$a, xx$b) > Error in get(x, envir, mode, inherits) : variable "xx$a" was not found > > Thank you. > > Christos Hatzis, Ph.D. > Nuvera Biosciences, Inc. > 400 West Cummings Park > Suite 5350 > Woburn, MA 01801 > Tel: 781-938-3830 > www.nuverabio.com > > ______________________________________________ > R-help at stat.math.ethz.ch 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.