【发布时间】:2016-01-31 17:11:43
【问题描述】:
【问题讨论】:
-
请勿以图片形式发布您的数据,请学习如何提供reproducible example
【问题讨论】:
我们可以从index 和format 中提取“月份”,通过与“Dec”比较得到一个逻辑索引,并使用它来子集zoo 对象。
z1[format(index(z1), '%b')=='Dec']
#1938-12-03 1938-12-10 1938-12-17 1938-12-24 1938-12-31
# 49 50 51 52 53
如果我们转换为xts 对象,也可以使用xts 包中的.indexmon。 .indexmon 从 0 开始,所以 12 月是 11。
library(xts)
z1[.indexmon(as.xts(z1))==11]
cmets 的其他选项是在 index 上使用 grep 来获取数字索引和子集(来自 @Pierre Lafortune)
z1[grep("-12-",index(z1))]
或使用subset/month 选项(来自@G. Grothendieck)
subset(z1, months(time(z1)) == "December")
library(zoo)
z1 <- zoo(1:100, order.by = seq(as.Date('1938-01-01'),
length.out=100, by = '1 week'))
【讨论】:
z1[grep("-12-",index(z1))]
subset(z1, months(time(z1)) == "December")