[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 16:11:20 CEST 2010


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_ID            Date 
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   4260501    2275  7/11/2007 
0:00          YTH       Ax1         A    F              1 21.5 
Spring           SS Heathy Dry Forest     N       -0.80          0.59 
0.48   NA        43           5
13   13         13      Axe Creek   3713602    1170  8/11/2007 
0:00          YTH       Ax1         A    F              1 21.5 
Spring           SS Heathy Dry Forest     N       -0.79          0.61 
0.53 20.4        43           5
19   19         21      Axe Creek   3713603    1171  9/11/2007 
0:00          YTH       Ax1         A    F              1 19.5 
Spring           SS Heathy Dry Forest     N       -0.78          0.62 
0.53   NA        43           5
30   30         34      Axe Creek   3713604    1172 10/11/2007 
0:00          YTH       Ax1         U    M              1 24.5 
Spring           SS Heathy Dry Forest     Y       -0.76          0.63   
NA   NA        43           5
31   31         35      Axe Creek   3713605    1173 10/11/2007 
0:00          YTH       Ax1         U    U              1   NA 
Spring           SS Heathy Dry Forest     U       -0.76          0.63   
NA   NA        43           5
32   32         36      Axe Creek   3713606    1174 10/11/2007 
0:00          YTH       Ax1         U    M              1 23.5 
Spring           SS Heathy Dry Forest     U       -0.76          0.63 
0.50   NA        43           5
    Litter_Cov Understorey TreeCov H.L WBC      BCI   CCIPca1 YEAR 
Hab_Config
10       22.5          65    0.35  NA  NA       NA        NA 2007          D
13       22.5          65    0.35  NA  NA -3.11592 0.6215803 2007          D
19       22.5          65    0.35  NA  NA       NA        NA 2007          D
30       22.5          65    0.35  NA  NA       NA        NA 2007          D
31       22.5          65    0.35  NA  NA       NA        NA 2007          D
32       22.5          65    0.35  NA  NA       NA        NA 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
>>
>
>



More information about the R-help mailing list