你循环里的i
是 1, 2, 3, 4, 5...
而不是每一个grid
dat <- read.csv("1500-2000LL.csv",header = T)
for (i in (1:length(unique(dat$grid)))) {
grid <- unique(dat$grid)[i]
treedat <- dat[dat$grid==grid]
write.csv(treedat,paste0(grid,".csv"),row.names=FALSE)
}
更R-style的做法可能是 (以iris
为例)
l <- split(iris,iris$Species)
mapply(write.csv,x=l,file=paste0(names(l),'.csv'),row.names = FALSE)