Ale*_*lex 48 r time-series subset xts
有非常好的方法来xts
对象进行子集化.例如,可以获取所有年,月,日的所有数据,但严格地在上午9:30到下午4点之间执行:
my_xts["T09:30/T16:00"]
Run Code Online (Sandbox Code Playgroud)
或者您可以通过以下方式获得两个日期之间的所有观察:
my_xts["2012-01-01/2012-03-31"]
Run Code Online (Sandbox Code Playgroud)
或者在某个特定日期之前/之后的所有日期:
my_xts["/2011"] # from start of data until end of 2011
my_xts["2011/"] # from 2011 until the end of the data
Run Code Online (Sandbox Code Playgroud)
如何获取所有年份的特定月份或所有月份和年份的特定日期的所有数据?是否存在任何其他子集技巧?
Jos*_*ich 39
您可以使用该.index*
系列函数来获取该月的某些月份或某些天.有关?index
功能的完整列表,请参阅.例如:
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
Run Code Online (Sandbox Code Playgroud)
GSe*_*See 15
时间子集有点隐藏,所以我理解为什么会引发这样的问题.我知道的唯一其他"技巧"是last
和first
函数,如果需要,可以嵌套.例如,这将获得前3周的最后2天.
last(first(my_xts, "3 weeks"), "2 days")
Run Code Online (Sandbox Code Playgroud)