Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

r - Return data subset time frames within another timeframes?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...