从 quantmod 循环 viewFinancials

Looping viewFinancials from quantmod

我需要一些关于 R 的 quantmod 包以及与 for 循环交互的帮助。 我有向量 year:

library(quantmod)
getFinancials("GE")
year <- colnames(viewFin(GE.f, "IS", "A"))
year
# [1] "2015-12-31" "2014-12-31" "2013-12-31" "2012-12-31"

viewFin 函数给我这个输出:

viewFin(GE.f, type="IS", period="A")["Net Income", year[1]]
# Annual Income Statement for GE
# [1] -6126

但是如果我尝试通过索引年份来循环我会得到这个错误:

> for(x in 1:4){
+   (viewFin(GE.f, type="IS", period="A")["Net Income", year[x]])
+   x=x+1
+ }

Annual Income Statement for GE
Annual Income Statement for GE
Annual Income Statement for GE
Annual Income Statement for GE
Error in viewFin(GE.f, type = "IS", period = "A")["Net Income", year[x]] : 
  subscript out of bounds

我的想法是制作日期、财务名称(净收入、股权等)和符号来创建一个数据框,行中是公司,列是年份和财务。

viewFin函数是否屏蔽了x值的入口?

请注意,您不能在 R 中的 for 循环内更改迭代器。因此,您在 for 循环末尾的 x = x + 1 是不必要的,将被忽略。另请注意,auto-printing 在 for 循环中被禁用,因此您需要显式调用 print。您可以做的另一件事是直接迭代一个向量(不需要子集)。所以你的 for 循环看起来像这样:

for(y in year) {
   print(viewFin(GE.f, type="IS", period="A")["Net Income", y])
}

也就是说,for 循环是不必要的。你可以直接使用子集得到相同的结果。

netIncome <- viewFin(GE.f, type="IS", period="A")["Net Income",]

data.frame 以符号作为行,以行项目和日期作为列来制作 data.frame 可能会有问题,因为没有理由每个符号都应该具有相同的数据年数,或者完全相同行项目。最好先将所有数据以长格式保存,直到您知道自己在使用什么。这是一个针对多个符号执行此操作的函数。

stackFinancials <-
function(symbols, type = c("BS", "IS", "CF"), period = c("A", "Q")) {
  type <- match.arg(toupper(type[1]), c("BS", "IS", "CF"))
  period <- match.arg(toupper(period[1]), c("A", "Q"))

  getOne <- function(symbol, type, period) {
    gf <- getFinancials(symbol, auto.assign = FALSE)
    vf <- viewFinancials(gf, type = type, period = period)
    df <- data.frame(vf, line.item = rownames(vf), type = type, period = period,
                     symbol = symbol, stringsAsFactors = FALSE, check.names = FALSE)
    long <- reshape(df, direction="long", varying=seq(ncol(vf)), v.names="value",
                    idvar="line.item", times=colnames(vf))
    rownames(long) <- NULL
    long
  }
  # combine all into one data.frame
  do.call(rbind, lapply(symbols, getOne, type = type, period = period))
}

这是一个使用它的例子:

R> Data <- stackFinancials(c("GE", "AAPL"), type = "IS", period = "A")
Annual Income Statement for GE
Annual Income Statement for AAPL
R> head(Data)
                               line.item type period symbol       time  value
1                                Revenue   IS      A     GE 2016-12-31 123693
2                   Other Revenue, Total   IS      A     GE 2016-12-31     NA
3                          Total Revenue   IS      A     GE 2016-12-31 123693
4                 Cost of Revenue, Total   IS      A     GE 2016-12-31  92508
5                           Gross Profit   IS      A     GE 2016-12-31  31185
6 Selling/General/Admin. Expenses, Total   IS      A     GE 2016-12-31  18377
R> tail(Data)
                                   line.item type period symbol       time value
387  Effect of Special Items on Income Taxes   IS      A   AAPL 2013-09-28    NA
388 Income Taxes Ex. Impact of Special Items   IS      A   AAPL 2013-09-28    NA
389            Normalized Income After Taxes   IS      A   AAPL 2013-09-28    NA
390        Normalized Income Avail to Common   IS      A   AAPL 2013-09-28    NA
391                     Basic Normalized EPS   IS      A   AAPL 2013-09-28    NA
392                   Diluted Normalized EPS   IS      A   AAPL 2013-09-28  5.68