在另一个时间范围内返回数据子集时间范围? [英] Return data subset time frames within another timeframes?

查看:52
本文介绍了在另一个时间范围内返回数据子集时间范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

子集xts 对象有非常漂亮的方法.例如,您可以通过以下方式获取所有年、月、日的所有数据,但严格限制在上午 9 点 30 分到下午 4 点之间:

There are very nifty ways of subsetting xts objects. For example, one can get all the data for all years, months, days but being strictly between 9:30 AM and 4 PM by doing:

my_xts["T09:30/T16:00"]

或者您可以通过执行以下操作获取两个日期之间的所有观察值:

Or you can get all the observations between two dates by doing:

my_xts["2012-01-01/2012-03-31"]

或者通过执行以下操作来获取某个日期之前/之后的所有日期:

Or all the dates before/after a certain date by doing:

my_xts["/2011"]  # from start of data until end of 2011
my_xts["2011/"]  # from 2011 until the end of the data

我如何才能获得所有年份中特定月份的所有数据或所有月份和年份中仅特定日期的所有数据?是否存在任何其他子集化技巧?

How can I get all the data for only certain months for all years or only certain days for all months and years? Do any other subsetting tricks exist?

推荐答案

您可以使用 .index* 系列函数来获取特定月份或特定日期.有关完整的函数列表,请参阅 ?index.例如:

You can use the .index* family of functions to get certain months or certain days of the month. See ?index for the full list of functions. For example:

library(quantmod)
getSymbols("SPY")
SPY[.indexmon(SPY)==0]   # January for all years (note zero-based indexing!)
SPY[.indexmday(SPY)==1]  # The first of every month
SPY[.indexwday(SPY)==1]  # All Mondays

这篇关于在另一个时间范围内返回数据子集时间范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆