来自quantmod的循环viewFinancials

时间:2017-02-05 01:47:46

标签: r 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的值?

1 个答案:

答案 0 :(得分:1)

请注意,您无法在R中的for循环内更改迭代器。因此,for循环结束时的x = x + 1是不必要的,并被忽略。另请注意,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可能会有问题,因为没有理由每个符号应该具有相同的年份数据,或者完全相同订单项。最好先将所有数据放在长格式中,直到你知道自己在做什么为止。这是一个为多个符号执行此操作的函数。

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