我正在尝试使用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
和输出&#39;全部&#39;是:
$`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
我想要两张纸&#34;收入&#34;,&#34;营业收入&#34;在xlsx中同时保持rownames和columnnames。但我无法找到一种方法来列表。 请帮忙
答案 0 :(得分: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