Exporting large list of dataframes to csv after split
You don't need to use get
here, and in general using get
is a bad practice.
Your object df_list
is a list of data frames. The first argument to write.csv
should be the data frame you want to save. So you can write your loop as:
paths = paste0(path, names, ".csv")
for (i in seq_along(df_list)) {
write.csv(df_list[[i]], paths[i], row.names = FALSE)
}
I lifted the paste0
call out of the loop because it's more efficient and idiomatic to use vectorization to create the file paths.
Exporting a list of dataframes as csv
Your problem is how you're indexing your list
object and names(listofdf)[i]
isn't doing what you're thinking. Try This:
listofdf <- list(a = iris, b = iris, c = iris)
for (i in seq_along(listofdf)){
write.csv(listofdf[[i]], file = paste0(names(listofdf)[i], ".csv"), row.names = F)
}
Side note: the default separator for paste
is a space. So you're putting a space before the ".csv" extension with your code. paste0
does not paste strings together with a space.
Alternatively, as mentioned you can use writexlsx
by simply:
library(writexl)
write_xlsx(listofdfs, "output.xlsx")
This will create a file called "output.xlsx" with sheets that match names(listofdfs)
and the proper values stored within those sheets.
Writing multiple data frames into .csv files using R
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))
Write list of data.frames to separate CSV files with lapply
Try this:
sapply(names(df.daily),
function (x) write.table(df.daily[[x]], file=paste(x, "txt", sep=".") ) )
You should see the names ("1", "2", "3") spit out one by one, but the NULLs are the evidence that the side-effect of writing to disk files was done. (Edit: changed [] to [[]].)
Related Topics
Shiny Dynamic Filter Variable Selection and Display of Variable Values for Selection
How to Extract All the Rows If a Level in One Column Contains All the Levels of Another Column in R
How to Change X-Axis Tick Label Names, Order and Boxplot Colour Using R Ggplot
How to Play Birthday Music Using R
Shift Legend into Empty Facets of a Faceted Plot in Ggplot2
Convert a Row of a Data Frame to Vector
Get the Path of Current Script
How to Increase the Size of Points in Legend of Ggplot2
Given a Set of Random Numbers Drawn from a Continuous Univariate Distribution, Find the Distribution
Importing Data into R from Google Spreadsheet
What Are 'User' and 'System' Times Measuring in R System.Time(Exp) Output
Auto Complete and Selection of Multiple Values in Text Box Shiny
Faster Way to Subset on Rows of a Data Frame in R
How to Build a Graph from a Data Frame Using the Igraph Package