Hi,
I would like to export a list object to an Excel sheet. i was wondering if this can be done with the WriteXLS package. in the documentation it says:
A character vector or factor containing the names of one or more R data frames; A character vector or factor containing the name of a single list which contains one or more R data frames; a single list object of one or more data frames; a single data frame object.
Lets say I have this list:
mylist = list()
mylist[["a"]] = 1:10
mylist[["b"]] = letters[1:10]
mylist[["c"]] = c(1,2,3,4,5,6)
When I try to export it with WriteXLS I get the following error:
Error in WriteXLS(ExcelFileName = "tmp.xls", x = mylist) :
One or more of the objects named in 'x' is not a data frame or does not exist**
Is there a way to automatically convert all the list elements into a data.frame on-the-fly and than export them into an XLS(X) object?
thanks
Assa
2 answers
Automatic? No, but you could lapply() a function to do that. Something like function(x) data.frame(foo=x) would work if you only have vector elements.
list:
mylist = list()
mylist[["a"]] = 1:10
mylist[["b"]] = letters[1:10]
mylist[["c"]] = c(1,2,3,4,5,6)
All data frames, appended together, in the same excel sheet:
library(xlsx)
write.xlsx(unlist(mylist),"temp.xlsx")
Each data frame into a separate excel sheet, but within a single excel file (3 sheets in a single excel file):
library(xlsx)
for (i in c(1:3)){
+ write.xlsx(mylist[i], file="test.xlsx", sheetName=paste(i), append=T)
+ }
Each data frame into separate excel file (total 3 excel files)::
library(xlsx)
for (i in c(1:3)){
+ write.xlsx(mylist[i], file=paste(i,"test.xlsx"))
+ }
Log in to answer this question.