[R] using get and paste in a loop to return objects for object names listed a strings

Nevil Amos nevil.amos at gmail.com
Thu Apr 29 09:53:15 CEST 2010


I am trying to create a heap of boxplots, by looping though a series of 
factors and variables in a large data.frame suing paste to constrcut the 
facto and response names from the colnames
I thought I could do this using get()
however it is not working what am I doing wrong?

thanks

Nevil Amos

> sp.codes=levels(data.all$CODE_LETTERS)
>
> for(spp in sp.codes) {
>
>
> data.sp=subset(data.all,CODE_LETTERS==spp)
>
> responses = colnames(data.all)[c(20,28,29,19)]
>  #if (spp=="BT") responses = colnames(data.all)[c(19,20,26:29)]
> groups=colnames   (data.all)[c(9,10,13,16,30)]
>
> data.sp=subset(data.all,CODE_LETTERS==spp)
> for (response in responses){
> for (group in groups){
> r<-get(paste("data.sp$",response,sep=""))
> g<-get(paste("data.sp$",group, sep=""))
> print (r)
> print(g)
>
> boxplot(r ~g)
> }}}
Error in get(paste("data.sp$", response, sep = "")) :
   object 'data.sp$Hb' not found



More information about the R-help mailing list