如何在 xts 包中使用 .indexyear

How to use .indexyear in xts package

我正在尝试使用 xts 包中的 .indexyear 函数,但无法理解它应该如何使用。

下面是一些代码,你可以看到 .indexyear returns 112, 113, 114, 1152012, 2013, 2014, 2015。我想查看xts对象索引中是否存在某个年份,那么如何让2012 %in% .indexyear(a)等于TRUE呢?

代码

Browse[1]> index(a)
[1] "2012-12-30 00:00:00 CET" "2013-12-30 00:00:00 CET" "2014-12-30 00:00:00 CET" "2015-12-30 01:00:00 CET"

Browse[1]> .indexyear(a)
[1] 112 113 114 115

Browse[1]> 2014 %in% .index(a) # should actually be TRUE!
[1] FALSE

Browse[1]> 113 %in% .indexyear(a)
[1] TRUE

.index* 函数基本上包装了 POSIXlt class 的组件。所以请参阅 ?POSIXltDetails 部分,其中显示:

'year' years since 1900.

所以你需要在 .indexyear 的输出中加上 1900 才能得到你想要的。

a <- structure(1:4, .Dim = c(4L, 1L), index = structure(c(1356847200, 1388383200,
  1419919200, 1451458800), tzone = "", tclass = c("POSIXct", "POSIXt")),
  class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"),
  tclass = c("POSIXct", "POSIXt"), .indexTZ = "UTC", tzone = "UTC")
2014 %in% (.indexyear(a)+1900)
# [1] TRUE