R : 刻度数据缺失时刻度数据加值 [英] R : Tick data adding value when tick data is missing

查看:14
本文介绍了R : 刻度数据缺失时刻度数据加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理刻度数据,并希望将我的 xts 不规则间隔系列聚合成一个 1 秒的同质系列.因此,我使用 xts 包函数 to.period :

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)

这是我得到的:

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

我的系列已汇总,但例如在 08:00:13 和 08:00:15 时缺少刻度数据.我想要的是用以前的分时数据填充这些空白,因为知道在逐个分时 xts 系列中缺少 08:00:13 和 08:00:15 的价格.有什么想法吗?

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?

谢谢

推荐答案

您可以将 price_1m 与空" xts 对象合并,该对象包含您想要的规则间隔的索引,使用 na.locf 就可以了.例如:

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)))

这篇关于R : 刻度数据缺失时刻度数据加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆