0

I have a list of dataframes which I want to export as csv. I tried :

for (i in listofdf){
  write.csv(listofdf[i], file = paste(names(listofdf)[i],".csv", sep=""), row.names = FALSE)
}

and got : Error in listofdf[i] : invalid subscript type 'list'. I read that I am feeding a list data type into a process that expects a vector, and tried to apply the given solution : unlist(listofdf), but all I got is a massive list of numeric values that I don't know how to deal with.

Then I tried a solution found here, that works with the given example, but when I try to apply :

sapply(names(listofdf),
       function (x) write.table(listofdf[x],
                                file = paste(names(listofdf)[x],".csv", sep=""),
                                row.names = FALSE))

but when I try it, it only exports one file named NA.csv. Do you know how to make any of those solutions work?

Anthony
  • 39
  • 6
  • Do they have to be a csv? If not, `writexl::write_xlsx` will write each element of a list to a sheet in an excel workbook. It's vectorized so you won't need a loop and it uses the list name as the sheetname. – LMc May 28 '21 at 17:46
  • Refer to elements of a list with `[[`, not `[`. – Limey May 28 '21 at 17:47
  • 1
    Also try `listofdf[[i]]` in your code – LMc May 28 '21 at 17:47
  • Csv is the format I'm used to but I can try something else. Same issue with one or two `[` `listofdf[[i]]` gives the same error (invalid subscript type 'list') but `listofdf[[1]]` works – Anthony May 28 '21 at 17:52

1 Answers1

3

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.

LMc
  • 12,577
  • 3
  • 31
  • 43
  • Same error with your first solution (invalid subscript type 'list'), but the second one works as you said. Can I import the sheets individually in R? – Anthony May 28 '21 at 18:05
  • @Anthony, I've made a small reproducible example that works. There are many ways you can import the sheets of an excel file. You can specify a specific sheet using `readxl::read_excel` or you can import the entire workbook into a `list` object where the list names are the sheet names using `rio::import_list`, for example. – LMc May 28 '21 at 18:10
  • Indeed, it worked with the iris dataset, still not with mine. I'll try to figure this out. The readxl solution will do the job. Thank you for your help – Anthony May 28 '21 at 18:17
  • @Anthony you're welcome. Unfortunately, without seeing more of your structure I cannot determine what is causing your issue. – LMc May 28 '21 at 18:18