简体   繁体   中英

Split dataframe in R by date

I have a data.frame that contains one Date type variable. I want to export 4 files, one containing a subset corresponding to each week. The following will divide my data in 4 however I don't know how to store each of this in a new data.frame.

split(DataAir, sample(rep(1:4)))

Thanks

If you save your split data frames in a variable. You can access the elements with double-bracket subsetting, (eg s[[1]] ). To save, create a vector of file names as you'd like and write each to file.

s <- split(iris, iris$Species)
filenames <- paste0("my_path/file", 1:3, ".csv")
for(i in 1:length(s)) write.csv(s[[i]], filenames[i])

And for R users that get unnecessarily bugged out by for loops:

mapply(function(x,y) write.csv(x,y), s, filenames)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM