[R] How to repeat code snippet for several variables in a data frame?

Sander Oom slist at oomvanlieshout.net
Mon Aug 15 22:51:01 CEST 2005


Dear all,

I have a data frame containing the results of an experiment. Like this:

a<-seq(1,4,by=1)
b<-seq(1,2,by=1)
test<-expand.grid(b,a,a)
colnames(test)<-c("replicates","bins", "groups")
test$abc <- rnorm(32)
test$def <- rnorm(32)
test$ghi <- rnorm(32)
test

The following code snippet aggregates the data for one variable and then 
draws a plot:

tmp <- aggregate(test$abc, list(
   test$bins, test$groups),
   mean)
colnames(tmp) <- c("bins", "groups", "abc")
tmp
#pltName <- paste("line_datGrassChem_", "abc", ".eps", sep="")
#postscript(pltName)
   labs <- c("-15/-9","-9/-6","-6/-3","-3/0")
   sps <- trellis.par.get("superpose.symbol")
   sps$pch <- 1:4
   trellis.par.set("superpose.symbol", sps)
   xyplot( abc ~ bins, data = tmp,
     groups = groups, type = list("p", "l"),
     scales = list(x = list(labels=labs)),
     layout = c(1,1),
     key = list(columns = 4,
       text = list(paste(unique(tmp$groups))),
       points = Rows(sps, 1:4)
       )
   )
#dev.off()

How can I wrap R code around this code snippet, such that I can repeat 
the same code snippet for all other variables in the data frame (i.e. 
def, ghi, etc.).

Thanks for your suggestions!

Sander.

-- 
--------------------------------------------
Dr Sander P. Oom
Animal, Plant and Environmental Sciences,
University of the Witwatersrand
Private Bag 3, Wits 2050, South Africa
Tel (work)      +27 (0)11 717 64 04
Tel (home)      +27 (0)18 297 44 51
Fax             +27 (0)18 299 24 64
Email   sander at oomvanlieshout.net
Web     www.oomvanlieshout.net/sander




More information about the R-help mailing list