lea
lea

Reputation: 143

How to save a list which has files into csv in R?

I have a list which has 12 data frames. How do I save each file within the list into separate csv files ? Also I am trying to save and name each file by its list name.

I tried to use laaply but I am not sure what i am doing wrong and it throws an error in file: Invalid description error.

x <- list of 12 df

lapply(x, function(x) write.csv(data.frame(x),paste("/path of the file","file_",names(x)[],".csv",sep=","), row.names=F))

Upvotes: 0

Views: 162

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389335

With base R lapply try

lapply(seq_along(x), function(i) write.csv(x[[i]], 
         paste0("/path/of/the/file/", names(x)[i], ".csv"), row.names = FALSE))

Or with imap

purrr::imap(x, write.csv(.x, 
               paste0("/path/of/the/file/", .y, ".csv"), row.names = FALSE))

Upvotes: 2

Related Questions