从R导出列表到Excel,同时维护列表名称和子列表名称

我试图从很多公司获得财务信息,同时使用R的包“quantmod”。 我在列表中得到了令人满意的结果,但我想将其导出到Excel文件中。 每个列表成员具有不同的电子表格,并且所述表格的名称是列表中的名称。 到目前为止,我有这个代码和输出:

library(quantmod) library(dplyr) library(tidyr) symbols <- stockSymbols()[,1:2] allfunc<-function(y) sapply(fin1, function(x) x$IS$A[y, ],USE.NAMES = TRUE)*10^6 s <- c("AAPL","GOOG","IBM","GS","AMZN","GE") want<-c("Total Revenue","Operating Income") fin1 <- lapply(s, getFinancials, auto.assign=FALSE,names=s) names(fin1)<-s all<- lapply(want,allfunc) ss<-sapply(want,allfunc) names(all)<-want all 

和输出“全部”是:

 $`Total Revenue` AAPL GOOG IBM GS AMZN GE 2016-09-24 2.15639e+11 9.0272e+10 7.9919e+10 3.7712e+10 1.35987e+11 1.23693e+11 2015-09-26 2.33715e+11 7.4989e+10 8.1741e+10 3.9395e+10 1.07006e+11 1.17385e+11 2014-09-27 1.82795e+11 6.6001e+10 9.2793e+10 4.0085e+10 8.89880e+10 1.17184e+11 2013-09-28 1.70910e+11 5.5519e+10 9.8367e+10 4.0874e+10 7.44520e+10 1.13244e+11 $`Operating Income` AAPL GOOG IBM GS AMZN GE 2016-09-24 6.0024e+10 2.3716e+10 1.0699e+10 1.0304e+10 4.186e+09 9.0300e+09 2015-09-26 7.1230e+10 1.9360e+10 1.5262e+10 8.7780e+09 2.233e+09 9.3470e+09 2014-09-27 5.2503e+10 1.6496e+10 1.9244e+10 1.2357e+10 1.780e+08 1.1348e+10 2013-09-28 4.8999e+10 1.5403e+10 1.9422e+10 1.1737e+10 7.450e+08 9.9490e+09 

我希望在保留rownames和列名的同时,在xlsx中有两张“收入”,“运营收入”。 但我无法find一个方法来做到这一点。 请帮忙

像这样的东西:

 library(xlsx) wb = createWorkbook() # Create an Excel workbook object lapply(names(all), function(s) { sht = createSheet(wb, s) # Add a sheet to the workbook using the name of the list element we want to save addDataFrame(all[[s]], sht) # Add the list element (ie, the data frame) to the current worksheet } saveWorkbook(wb, "my_workbook.xlsx") # Save the workbook as a Excel file