[R] get() with complex objects?

jim holtman jholtman at gmail.com
Fri Jul 27 19:19:50 CEST 2007


'get' tries to retrieve the object given by the character string.  The
error message says that object can not be found.  You actually have to
'evaluate' the character string.  See the example below:

> x <- data.frame(a=1:10, b=11:20)
> x$a
 [1]  1  2  3  4  5  6  7  8  9 10
> z <- 'x$a'
> get(z)
Error in get(x, envir, mode, inherits) : variable "x$a" was not found
> # parse and evaluate the character string 'x$a'
> eval(parse(text=z))
 [1]  1  2  3  4  5  6  7  8  9 10

Does this make sense?


On 7/27/07, Mark Orr <mo2259 at columbia.edu> wrote:
> Hello R-listers,
> I'm having trouble accessing "sub" objects ("attributes"?), e.g.,
> "x$silinfo$avg.width" using the /get() /command;  I'm using/ get()/ in a
> loop as illustrated in the following code:
>
> #FIRST MAKE CLUSTERS of VARYING  k
> /for (i in 1:300){
>  assign(paste("x.",i,sep=""),pam(x,i))  #WORKS FINE
> }/
>
> #NEXT, TAKE LOOK AT AVE. SILHOUETTE VALUE FOR EACH k
>
> #PART 1, MAKE LIST OF OBJECTS NEEDED
> /gen.list <- rep("t",300)
> for (i in 1:300){
>  assign(gen.list[i],paste("x.",i,"$silinfo$avg.width",sep=""))
> }
> #WORKS FINE
>
> /#PART 2, USE LIST IN LOOP TO ACCESS OBJECT.
> /si//l.collector <- rep(99,300)
> for(i in 1:300){
>  sil.collector <- get(gen.list[i])
> }/
> #HERE IS THE ERROR
> /Error in get(x, envir, mode, inherits) : variable
> "x.1$silinfo$avg.width" was not found
>
> /So, I get the gist of this error; x.1 is an object findable from get(),
> but the "attribute"  levels are not accessible.  Any suggestions on how
> to get get() to access these levels?  From reading the get()'s help
> page, I don't think it will access the attributes. (my apologies for
> loosely using the term attributes, but I hope it is clear).
>
> Thanks,
>
> Mark Orr
>
> --
> ***********************************************
> Mark G. Orr, PhD
> Heilbrunn Dept. of Population and Family Health
> Columbia University
> 60 Haven Ave., B-2
> New York, NY 10032
>
> Tele: 212-304-7823
> Fax:  212-305-7024
>
> www.columbia.edu/~mo2259
>
>
> ______________________________________________
> 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.
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list