export list objects to XLS files
2
0
Entering edit mode
8.1 years ago
Assa Yeroslaviz ★ 1.9k

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

WriteXLS xlsx lists • 22k views
ADD COMMENT
0
Entering edit mode
8.1 years ago

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.

ADD COMMENT
0
Entering edit mode
8.1 years ago
ssv.bio ▴ 200

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"))
+ }
ADD COMMENT

Login before adding your answer.

Traffic: 2554 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6