[R] using get and paste in a loop to return objects for object names listed a strings
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 __ R-help@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.
Re: [R] using get and paste in a loop to return objects for object names listed a strings
Nevil Amos wrote: 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? You don't give a reproducible example, this makes it hard to answer your question. But not really in response to your question, take a look at histogram from the lattice package or geom_boxplot from the ggplot2 package. These functions can do all the work for you of drawing boxplots for a series of factors and variables in a large data.frame. This saves you a lot of time. cheers, Paul 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 __ R-help@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. -- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +3130 274 3113 Mon-Tue Phone: +3130 253 5773 Wed-Fri http://intamap.geo.uu.nl/~paul http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770 __ R-help@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.
Re: [R] using get and paste in a loop to return objects for object names listed a strings
Thanks for that, the package looks very useful. It gave me the answer in a roundabout way - reminded me I needed to sue attach() so that the get () was only dealing with the objects in data.frame, rather than using the data.frame$factorname I therefore managed to sort a work around, but will be looking at ggplot 2 for other things the work around and the head of the data file is shown below head(data.all) Line Capture_ID Landscape_Name Band_text Bird_IDDate CODE_LETTERS Site_Name Age_Class SEX_ Capture_Number Mass Season SEASON_CLASS EVC Moult Sine_Julian Cosine_Julian HCT Hb Site_Cond Logs_Length 10 10 10 Axe Creek 42605012275 7/11/2007 0:00 YTH Ax1 AF 1 21.5 Spring SS Heathy Dry Forest N -0.80 0.59 0.48 NA43 5 13 13 13 Axe Creek 37136021170 8/11/2007 0:00 YTH Ax1 AF 1 21.5 Spring SS Heathy Dry Forest N -0.79 0.61 0.53 20.443 5 19 19 21 Axe Creek 37136031171 9/11/2007 0:00 YTH Ax1 AF 1 19.5 Spring SS Heathy Dry Forest N -0.78 0.62 0.53 NA43 5 30 30 34 Axe Creek 37136041172 10/11/2007 0:00 YTH Ax1 UM 1 24.5 Spring SS Heathy Dry Forest Y -0.76 0.63 NA NA43 5 31 31 35 Axe Creek 37136051173 10/11/2007 0:00 YTH Ax1 UU 1 NA Spring SS Heathy Dry Forest U -0.76 0.63 NA NA43 5 32 32 36 Axe Creek 37136061174 10/11/2007 0:00 YTH Ax1 UM 1 23.5 Spring SS Heathy Dry Forest U -0.76 0.63 0.50 NA43 5 Litter_Cov Understorey TreeCov H.L WBC BCI CCIPca1 YEAR Hab_Config 10 22.5 650.35 NA NA NANA 2007 D 13 22.5 650.35 NA NA -3.11592 0.6215803 2007 D 19 22.5 650.35 NA NA NANA 2007 D 30 22.5 650.35 NA NA NANA 2007 D 31 22.5 650.35 NA NA NANA 2007 D 32 22.5 650.35 NA NA NANA 2007 D 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 attach(data.sp) for (response in responses){ for (group in groups){ g=get(group) r=get(response) boxplot(r ~g, main=spp,xlab=group,ylab=response) } } detach(data.sp) } On 29/04/2010 7:05 PM, Paul Hiemstra wrote: Nevil Amos wrote: 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? You don't give a reproducible example, this makes it hard to answer your question. But not really in response to your question, take a look at histogram from the lattice package or geom_boxplot from the ggplot2 package. These functions can do all the work for you of drawing boxplots for a series of factors and variables in a large data.frame. This saves you a lot of time. cheers, Paul __ R-help@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.