How do i extract a specific, recurring time from 1

2019-04-12 16:20发布

For instance, let's say I want to extract the price at 09:04:00 everyday from a timeseries that is formatted as:

DateTime            | Price
2011-04-09 09:01:00 | 100.00
2011-04-09 09:02:00 | 100.10
2011-04-09 09:03:00 | 100.13

(NB: there is no | in the actual data, i've just included it here to illustrate that the DateTime is the index and Price is the coredata and that the two are distinct within the xts object)

and put those extracted values into an xts vector...what is the most efficient way to do this?

Also, if i have a five year time series of a cross-border spread, where - due to time differences - the spread opens at different times during the year (say 9am during winter, and 10am during summer) how can I get R to take account of those time differences and recognise either 9am-16:30 or 10am-16:30 as the same "day" interval.

In other words, I want to convert an intraday, 1m tick data file to daily OHLC data. Normally would just use xts and to.period to do this, but - given the time difference noted above - gives odd / strange day start/end times due

Any advice greatly appreciated!

1条回答
贪生不怕死
2楼-- · 2019-04-12 16:23

You can use the "T" prefix with xts subsetting to specify a time interval for each day. You must specify an interval; a single time will not work.

set.seed(21)
x <- xts(cumprod(1+rnorm(2*60*24)/100),
  as.POSIXct("2011-04-09 09:01:00")+60*(1:(2*60*24)))
x["T09:01:59/T09:02:01"]
#                          [,1]
# 2011-04-09 09:02:00 0.9980737
# 2011-04-10 09:02:00 1.0778835
查看更多
登录 后发表回答