[R] Mutliple sets of data in one dataset....Need a loop?

Glen Sargeant gsargeant at usgs.gov
Fri Jan 22 17:02:22 CET 2010


Thank you, Mario.

Biostudent asked how one could perform repetitive tasks, e.g., plotting,
with subsets of data.  I originally provided a flexible example based on
lapply.  Mario suggested a variation that permits flexible control of
options.  This reply shows how Mario's objective and naming of each set of
results can be accomplished very simply, within the context of the approach
I suggested.

In practice, I often use a list of levels of my grouping variable, rather
than my list of data, as an argument to lapply().  Then I use the levels as
subscripts and labels.  For example:

#List of unique values for grouping variable 
#that is not necessarily a factor 
names <- as.list(unique(df$Experiment))

#List of colors, same length as 'names'
#In actual application, color1 , color2, etc.
#would be character strings, numbers, or
#color codes.
clr <- as.list(c(color1, color2, ...))
names(clr) <- names 

#List of dataframes; 1 for each unique value of grouping variable 
df.lst <- lapply(names,function(name)subset(df,Experiment==name)) 

#Name components of the list 
#Permits indexing by level of the grouping variable
names(df.lst) <- names 

#Now--if I didn't mistype something--lapply() can be used 
#to perform repetitive tasks without sacrificing flexibility.
#For example, to send plots to a pdf with 1 page for each 
#component, vary the color of points in each plot, and print 
#the value of the grouping variable at the top of each plot: 
pdf("plot.pdf") 
lapply(names,function(nms){
  plot(df.lst[[nms]][,2], df.lst[[nms]][,3],col=clr[[nms]])
  mtext(nms)}) 
dev.off() 


-----
Glen Sargeant
Research Wildlife Biologist
-- 
View this message in context: http://n4.nabble.com/Mutliple-sets-of-data-in-one-dataset-Need-a-loop-tp1018503p1100167.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list