You can use isplit
(from the "iterators" package) to create an iterator object that loops over the blocks defined by the site
column:
require(iterators)
site.data <- read.table("isplit-data.txt",header=T)
sites <- isplit(site.data,site.data$site)
Then you can use foreach
(from the "foreach" package) to create a plot within each block:
require(foreach)
foreach(site=sites) %dopar% {
pdf(paste(site$key[[1]],".pdf",sep=""))
plot(site$value$year,site$value$peak,main=site$key[[1]])
dev.off()
}
As a bonus, if you have a multiprocessor machine and call registerDoMC()
first (from the "doMC" package), the loops will run in parallel, speeding things up. More details in this Revolutions blog post: Block-processing a data frame with isplit
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…