[R] Using the get() function on an object within a list

David Winsemius dwinsemius at comcast.net
Tue Mar 30 23:45:23 CEST 2010


On Mar 30, 2010, at 5:23 PM, klc wrote:

>
> Hi-
>
> I am trying use the get function to return the value of an object  
> within a
> list.  As an abbreviated example:
>
> "original" represents a list of several objects, one of which is "a. 
> 1".
>   original<-vector('list',5)
>   names(original) <- c("a.1","a.2","a.3","b.1","b.2")
>   original$a.1 <- c(4,3)
>
> When I enter:
>  original$a.1
> R returns the following vector
>  [1] 4 3
> however, when I enter:
>  get("original$a.1")

 > get("original")$a.1
[1] 4 3

The get function will not parse and evaluate complex expressions for  
you. And the "$" operator is probably not as useful for this  
application as "[[" would be. See below

> R returns the following error:
>  "Error in get("original$a.1") : object 'original$a.1' not found"
>
> Does anyone happen to know how to resolve this error?
>
> I pose this question because ultimately I would like to use the get  
> function
> to convert particular objects within "original" into separate lists  
> as:
>   species <- c(1,2,3)
>   A <- vector('list',length(species))
>   A <- lapply(paste("original$a.", species, sep=""), get)

 > sapply(names(original), function(x) original[[x]])
$a.1
[1] 4 3

$a.2
NULL

$a.3
NULL

$b.1
NULL

$b.2
NULL

 > sapply(1:3, function(x) original[[x]])
[[1]]
[1] 4 3

[[2]]
NULL

[[3]]
NULL


>
> I apologize if I missed this question in a previous post.
>
> Many thanks-
> -- 
> View this message in context: http://n4.nabble.com/Using-the-get-function-on-an-object-within-a-list-tp1745965p1745965.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list