Writing multiple data frames into .csv files using R
This is a common operation. You need to split the dataframe into a list
of dataframes then write them to many separate csvs. I will demonstrate 2 approaches with base R, and 2 approaches with tidyverse.
base R
A for
loop makes the iteration very explicit.
# example data.frame
df <- data.frame(x = 1:4, y = c("a", "a", "b", "b"))
# split the dataframe into a list by the y column
l <- split(df, df$y)
# make filepaths from list names, which are unique values of the y column
file_out <- paste0(names(l), ".csv")
# iterate over the list and the vector of list names to write csvs
for(i in 1:length(l)) {
write_csv(l[[i]], file_out[i])
}
Or using mapply()
:
mapply(
function(x, y) write_csv(x, y),
l,
file_out
)
tidyverse approach
library(tidyverse)
# we pass walk2 two inputs: a list of dataframes (.x) and filepaths (.y)
# `walk` is a silent `map` that doesn't print output to the console
walk2(l, file_out, ~write_csv(.x, .y))
Or, avoiding intermediate variables:
df %>%
group_split(y) %>%
walk(~write_csv(.x, paste0(.x$y[1], ".csv")))
Here's a self-contained example along the lines of Richard's comment, but uses the names of the dataframes in the list as filenames for the CSV files:
# Create a list of n data frames
n <- 10
my_list <- lapply(1:n, function(i) data.frame(x = rnorm(10), y = rnorm(10)) )
# name the data frames
names(my_list) <- letters[1:n]
# save each new data frame as an individual .csv file based on its name
lapply(1:length(my_list), function(i) write.csv(my_list[[i]],
file = paste0(names(my_list[i]), ".csv"),
row.names = FALSE))