问题
I'm working on tick data and want to aggregate my xts irregularly spaced series into a 1 second homogeneous one. I thus use the xts package function to.period :
price_1m <-to.period(price,period="seconds",k=1,OHLC=FALSE)
here is what I get :
2010-02-02 08:00:03 2787
2010-02-02 08:00:04 2786
2010-02-02 08:00:05 2787
2010-02-02 08:00:06 2787
2010-02-02 08:00:07 2786
2010-02-02 08:00:08 2786
2010-02-02 08:00:09 2786
2010-02-02 08:00:10 2787
2010-02-02 08:00:11 2786
2010-02-02 08:00:14 2786
2010-02-02 08:00:16 2786
2010-02-02 08:00:18 2787
My series is aggregated but for example tick data is missing at times 08:00:13 and 08:00:15. What I want is to fill those blanks with previous tick data knowing that the 08:00:13 and 08:00:15 prices are missing in the tick-by-tick xts series. Any idea?
thanks
回答1:
You can merge price_1m
with an "empty" xts object that contains an index with the regularly-spaced intervals you want, use na.locf
on that. For example:
onemin <- seq(start(price_1m),end(price_1m),by="1 s")
Price_1m <- na.locf(merge(price_1m, xts(,onemin)))
回答2:
The MakeStrictlyRegular
function in my qmao package will do this for you.
Here is the example from ?MakeStrictlyRegular
x <- align.time(.xts(1:1000, 60*1:1000))[-c(2, 4, 7, 8), ] # remove some rows at the begining
head(x[paste((start.x <- start(x)), "/")])
# [,1]
#1969-12-31 18:02:00 1
#1969-12-31 18:04:00 3
#1969-12-31 18:06:00 5
#1969-12-31 18:07:00 6
#1969-12-31 18:10:00 9
#1969-12-31 18:11:00 10
x2 <- MakeStrictlyRegular(x)
#added 4 (0.40%); There are now 1000 total rows.
head(x2[paste(start.x, "/")])
# [,1]
#1969-12-31 18:02:00 1
#1969-12-31 18:03:00 1
#1969-12-31 18:04:00 3
#1969-12-31 18:05:00 3
#1969-12-31 18:06:00 5
#1969-12-31 18:07:00 6
For your 1 second data, you'd use by="sec"
. So, something like
MakeStrictlyRegular(price, by="sec")
来源:https://stackoverflow.com/questions/11669945/r-tick-data-adding-value-when-tick-data-is-missing