Question: export list objects to XLS files
0
gravatar for Assa Yeroslaviz
3.3 years ago by
Assa Yeroslaviz1.3k
Munich
Assa Yeroslaviz1.3k wrote:

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

lists xlsx writexls • 8.5k views
ADD COMMENTlink modified 3.3 years ago by ssv.bio180 • written 3.3 years ago by Assa Yeroslaviz1.3k
0
gravatar for Devon Ryan
3.3 years ago by
Devon Ryan94k
Freiburg, Germany
Devon Ryan94k wrote:

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 COMMENTlink written 3.3 years ago by Devon Ryan94k
0
gravatar for ssv.bio
3.3 years ago by
ssv.bio180
ssv.bio180 wrote:

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 COMMENTlink modified 3.3 years ago • written 3.3 years ago by ssv.bio180
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 801 users visited in the last hour