Skip to content

Instantly share code, notes, and snippets.

@jflanaga
Last active June 8, 2023 12:02
Show Gist options
  • Save jflanaga/1ab2fa1434064780d2237e73d9e669c4 to your computer and use it in GitHub Desktop.
Save jflanaga/1ab2fa1434064780d2237e73d9e669c4 to your computer and use it in GitHub Desktop.
R Script for splitting data frame and then saving separate .csv
#----------------------------------------------------------------------------------------
# File:
# Author: Joseph Flanagan, adopted from https://stackoverflow.com/questions/10002021/split-dataframe-into-multiple-output-files-in-r
# email: [email protected]
# Purpose: Split a dataframe by group, then save each as separate .csv file
#----------------------------------------------------------------------------------------
# new tidyverse solution with `group_walk`
library(dplyr)
library(readr)
iris %>%
group_by(Species) %>%
group_walk(~ write_csv(.x, paste0(.y$Species, ".csv")))
# Old version
library(tidyverse)
# Make a copy of iris
iris2 <- iris
# Split by variable
spt2 <- split(iris2, iris2$Species)
# Save
lapply(names(spt2), function(x){
write_csv(spt2[[x]], paste(x, ".csv", sep = ""))
})
@jflanaga
Copy link
Author

The "path" argument inside of write_csv is deprecated

Thanks for the update. I just saw that path = is deprecated and we should now use file = . I updated it so just to avoid the issue completely (although my preference is for arguments after the first to be named).

I also updated the original solution it to take advance of group_walk from dplyr. It's now a much cleaner solution that doesn't rely upon lapply() at all.

I prefer to use readr for most cases just for convenience, but obviously there are a range of alternatives and people can always find another package for their own use cases.

@endlesstour
Copy link

thank you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment