在 xts 包内使用 apply.yearly 时出错。这是如何工作的?

error using apply.yearly inside xts package. How this works?

我使用 ts 对象有一段时间了,但没有使用太多 xts,所以我试图计算经济时间序列中每年的均值、总和、最后、第一...(有趣)。当我尝试时,我得到了奇怪的结果。

这是一个简单的例子: 考虑 x 和 y 像月度系列,连续两年:

x <- 1:12
y <- 1:12
serie <- c(x,y)

然后我创建一个ts对象

serie <- ts(serie,1990,,12)

现在我将它转换为 xts 对象

library(xts)

serie.xts <- as.xts(serie)

plot(serie.xts)

据我所知,该系列已正确创建

serie.xts

这个系列看起来不错

ene 1990    1
feb 1990    2
mar 1990    3
abr 1990    4
may 1990    5
jun 1990    6
jul 1990    7
ago 1990    8
sep 1990    9
oct 1990   10
nov 1990   11
dic 1990   12
ene 1991    1
feb 1991    2
mar 1991    3
abr 1991    4
may 1991    5
jun 1991    6
jul 1991    7
ago 1991    8
sep 1991    9
oct 1991   10
nov 1991   11
dic 1991   12

然后我的问题来了

例如,我尝试获取每年的最后一个值,据我所知,这是该函数的作用:

apply.yearly(serie.xts,FUN = "last")

但我明白了:

ene 1990    1
ene 1991    1
dic 1991   12

我期待的是: 迪克 1990 12 dic 1991 12

我做错了什么? 这是这个功能的工作方式吗? 这个功能实际上是做什么的?

我不明白这是什么结果。 请帮忙。感谢您阅读我的问题。

R 3.3.3 xts 0.9-7 动物园 1.8-0

这是xts 0-9.7的bug,开发版已经修复了。 运行 从 GitHub 安装这些命令之一:

remotes::install_github("joshuaulrich/xts")
# or
devtools::install_github("joshuaulrich/xts")

然后 apply.yearly 调用将按您预期的那样工作:

serie <- ts(c(1:12,1:12),1990,,12)
library(xts)
serie.xts <- as.xts(serie)
apply.yearly(serie.xts, last)
#          [,1]
# Dec 1990   12
# Dec 1991   12