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

Glen Sargeant gsargeant at usgs.gov
Wed Jan 20 21:18:56 CET 2010


One way to plot subsets of data identified by a grouping variable is to use
lapply() on a list of subsets.  The approach is worth mentioning because
similar tactics are useful for many problems. 

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

#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
#Not necessary in this case... but permits indexing by level
#of the grouping variable
names(df.lst) <- names

#Now you can use lapply() to carry out the same operation on
#each component of your list.  For example, to send plots to
#a pdf with 1 page for each component:

pdf("plot.pdf")
lapply(df.lst,function(df)plot(df[,2],df[,3]))
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-tp1018503p1018714.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list