【问题标题】:exporting a list from R to Excel while maintaining list names and sub lists names将列表从 R 导出到 Excel,同时维护列表名称和子列表名称
【发布时间】:2017-02-03 17:29:17
【问题描述】:

我正在尝试使用 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

我想在 xlsx 中有两张“收入”、“营业收入”,同时保持行名和列名。但我找不到对列表执行此操作的方法。 请帮忙

【问题讨论】:

    标签: r excel list quantmod


    【解决方案1】:

    类似这样的:

    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 (i.e., the data frame) to the current worksheet
    }
    
    saveWorkbook(wb, "my_workbook.xlsx")  # Save the workbook as a Excel file
    

    【讨论】:

      猜你喜欢
      • 2022-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      相关资源
      最近更新 更多