【问题标题】:subset xts zoo to obtain only one month子集 xts zoo 仅获取 1 个月
【发布时间】:2018-07-13 09:45:42
【问题描述】:

我有一个每月 xts/zoo 时间序列对象,我想对其进行子集化,以便仅获得 11 月份的值。因此,对于每一年,我只想要 11 月。这是对象(已下载):

library(quantmod)
indPro<-getSymbols('INDPRO', src="FRED", auto.assign=FALSE)

有人知道怎么做吗?

这是数据的样子:

           INDPRO
1919-01-01 5.0346
1919-02-01 4.8121
1919-03-01 4.6730

【问题讨论】:

    标签: r subset xts zoo


    【解决方案1】:

    试试这个:

    indPro[.indexmon(indPro) == 10,]
    

    0 是 1 月,1 是 2 月,... 11 是 12 月。

    您还发现了解以下内容很有用:

    .indexyear
    .indexday
    .indexweek
    .indexhour
    .indexmin
    .indexsec
    

    查看?.indexsec 的帮助以了解更多信息。

    【讨论】:

    • 很好的答案,不知道这个功能!
    【解决方案2】:

    您的数据是什么样的?

    假设您的数据看起来有点像: df$Date = c(10-01-2015 12:22:00 等)

    我过去使用过dplyrzoo - 试试df &lt;- df %&gt;% filter(month(Date) == 11)

    【讨论】:

    • 我提供了上面的数据,可以使用 quantmod 包下载。日期可通过以下方式获得:dates
    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 2012-06-23
    • 2021-07-30
    • 2011-09-21
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多