Created
August 14, 2018 14:25
-
-
Save eddjberry/6dcda168c52204e27b91fbfc0ffdb246 to your computer and use it in GitHub Desktop.
Create separate csv files of the data for each level of some categorical column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
# Nest iris by Species | |
iris_nest <- iris %>% | |
group_by(Species) %>% | |
nest() | |
# Get the data list and set the names of the list to Species | |
# write_csv for each df in the data list with its name as the filename | |
iris_nest %>% | |
pull(data) %>% | |
set_names(iris_nest$Species) %>% | |
walk2(names(.), ~ write_csv(.x, path = str_c('data/', .y, '.csv'))) | |
# Another way to do the nesting is | |
iris %>% | |
nest(-Species) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment