子集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更多推荐
在另一个时间范围内返回数据子集时间范围?
发布评论