I'm trying to create separate .csv files for each group in a data frame grouped with dplyr's group_by function. So far I have something like
by_cyl <- group_by(mtcars, cyl)
do(by_cyl, write_csv(., "test.csv"))
As expected, this writes a single .csv file with only the data from the last group. How can I modify this to write multiple .csv files, each with filenames that include cyl?
You can wrap the csv write process in a custom function as follows. Note that the function has to return
a data.frame
else it returns an error Error: Results are not data frames at positions
This will return 3 csv files named "mtcars_cyl_4.csv","mtcars_cyl_6.csv" and "mtcars_cyl_8.csv"
customFun = function(DF) {
write.csv(DF,paste0("mtcars_cyl_",unique(DF$cyl),".csv"))
return(DF)
}
mtcars %>%
group_by(cyl) %>%
do(customFun(.))