Writing multiple data frames into .csv files using R
Solution 1
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))
Solution 2
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")))
Solution 3
In case this helps: I had an environment with multiple data frames, and only those data frames, and I wanted to output each data frame as a separate CSV file. With the help of Ben's answer, and discovering mget
, I was able to do that with the following code:
for(i in 1:length(ls())) {
write.table(
mget(ls()[[i]]),
file = paste0(ls()[[i]], ".csv"),
sep = ";",
qmethod = "double",
row.names = FALSE)
}
David Johnson
Graduate student in psychology at Michigan State University. User of R, primarily for bayesian estimation.
Updated on July 23, 2022Comments
-
David Johnson almost 2 years
I have used lapply to apply a function to a number of data frames:
data.cleaned <- lapply(data.list, shooter_cleaning)
And then labeled each of the resulting data frames in the list according to their subject number (e.g., 100):
names(data.cleaned) <- subject.names
What I want to do is to save each new data frame as an individual .csv file based on its subject number. For example, for subject 100 I'd like the .csv file to be labeled as "100.csv" Normally to do this (for a single data frame) I would just write (where x is the data frame):
write.csv(x, "100.csv", row.names = F)
But, obviously using lapply to do this for my list of data frames will just produce many copies of "100.csv" when instead I would like the files to be unique, based on their subject number. How can I (use apply to?) save each of these data frames to their own unique .csv file?
-
David Johnson over 9 yearsThanks! This individually exports the data frames like Richard's code above and also preserves the subject number associated with each data frame.