【问题标题】:R: Apply.yearly sums from Jan non inclusive to Jan in the following year inclusive. How could I make it sum from Dec to Dec?R:Apply.yearly 从一月(不含)到次年一月(含)的总和。我怎样才能从 12 月到 12 月求和?
【发布时间】:2016-07-14 13:37:02
【问题描述】:

在 R 中,以下代码:

apply.yearly(XTS_object, sum)

返回从第一年(不包括在内)的一月到下一年(包括在内)的总和。

我如何计算从 12 月到 12 月的总和?

【问题讨论】:

  • 请提供一个可重现的小示例和基于此的预期输出。
  • 搜索“季节性”或“财政年度”。已经有几个答案可以将 Dates 的划分转换为索引参数。您还需要查看apply.yearly 的代码。一旦你拥有了这两部分,就非常简单了......如果你和我们一样是一个“编码爱好者”,对吧?

标签: r xts


【解决方案1】:

提取endpoints 并与period.apply 一起使用:

getSymbols("SPY", from = "2013-12-01")
# Use your own data. Assuming Data already starts from December, otherwise extract all months and get Decembers only:
december <- endpoints(SPY, k = 12)
[1]   0 251 503

SPY[251 + 1]
               SPY.Open   SPY.High    SPY.Low  SPY.Close SPY.Volume SPY.Adjusted
    2014-12-01 206.399994 206.539993 205.380005 205.759995  103968400   199.464546

period.apply(Ad(SPY), december + 1, sum)

【讨论】:

  • 谢谢,在任何函数中都没有预先构建的参数可以做同样的事情吗?
  • period.apply 的问题在于它丢失了 xts 对象格式。确实不太理想。那里没有别的东西吗?另外,为什么没有人在 apply.yearly 中抱怨这个错误?
  • 我不太确定,为什么你认为这是一个错误。也许,我误会你了。请提供可重现的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 2021-01-23
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多