Write List of Data.Frames to Separate CSV Files with Lapply

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 [[]].)

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.csv on a list of data frames

The problem is that the input x is a number not the element of a list.
Try

 lapply(1:length(na_sx),  function (x) write.csv(na_s[[x]],file = paste('./na_s_daily/','na_', names (na_s[x]),'.csv',sep=""), row.names = F)) 

Note that the above will also return a list of the data frames. So if you just need to save each element of the list as a data frame in your directory just do

for(x in 1:length(na_s)){
write.csv(na_s[[x]],file = paste('./na_s_daily/','na_',names (na_s[x]),'.csv',sep=""), row.names = F)
}

Save each dataframe in a list as a CSV, and make the filename the same as the df name - R; lapply

If you want to iterate both names and data.frames, you are better off using mapply to walk both lists at the same time

mapply(function(dname, data) 
write.csv(data, file = paste0("C:/home/", dname, ".csv"), row.names = FALSE),
names(df), df)

when iterating a list via lapply() the current value name is not available. An altertative using lapply is to iterate the names, not the values

lapply(names(df), function(dname) 
write.csv(df[[dname]], file = paste0("C:/home/", dname, ".csv"), row.names = FALSE))

How do I write multiple CSV files from a list of data frames?

A few of things.

  1. myList doesn't have any names, so you aren't actually naming your files.
  2. Once you give myList names, you would do better to index along the names than along the list
  3. You use df in your for loop, but that isn't defined anywhere.

This should work (untested)

myList <- list(diamonds = diamonds, 
cars = cars)
for(i in names(mylist)){
write.csv(myList[[i]], paste0(i,".csv"))
}

Write list of data frames to CSVs in R

With base R you can do.

lapply(names(mylist), function(x) {write.csv(mylist[[x]], file = paste0(x, ".csv"))})

Or you can use the shortcut command iwalk from the tidyverse library purrr.

iwalk(mylist, function(x, y) {write.csv(x, file = paste0(y, ".csv"))})

## Or this alternative syntax.

iwalk(mylist, ~write.csv(.x, file = paste0(.y, ".csv")))

In R, how to read a list of filenames into individual data frames?

You can read in the data using list.files and lapply, as suggested in the OP comments. To make each list item a separate data frame, use the assign() function in a for loop:

d <- split(iris, f = iris$Species)

for (i in names(d)) {
assign(i, d[[i]])
}

This uses the list names as the newly assigned variable name, so make sure this is set appropriately.

writing a list of dataframes to file with lapply: How to paste object names to file names?

You are looping over the list elements, and not the names of the list elements. So the expression paste(x,".txt") tries to paste a data.frame to .txt, but you want to paste the name of the list element to .txt. So you can try the following:

lists <- replicate(10, as.data.frame(matrix(rnorm(100), 10, 10)), simplify = FALSE)
names(lists) <- LETTERS[1:10]

lapply(names(lists), function(x) write.table(lists[[x]], file=paste(x,".txt"), sep="\t"))

To suppress the console output, wrap the statement in invisible(), or alternatively use a regular for-loop:

for (x in names(lists))
write.table(lists[[x]], file=paste(x,".txt"), sep="\t")


Related Topics



Leave a reply



Submit