简体   繁体   English

如何从随机时间观察中生成常规xts周期?

[英]How to generate regular xts periods from random time observations?

I have the following xts matrix: 我有以下xts矩阵:

> options(digits.secs = 6)
> set.seed(1234)
> xts(1:10, as.POSIXlt(1366039619, tz="EST", origin="1970-01-01") + rnorm(10, 500000, 250000)/1000000)
                           [,1]
2013-04-15 10:26:58.913576    4
2013-04-15 10:26:59.198234    1
2013-04-15 10:26:59.277491   10
2013-04-15 10:26:59.356315    7
2013-04-15 10:26:59.358887    9
2013-04-15 10:26:59.363342    8
2013-04-15 10:26:59.569357    2
2013-04-15 10:26:59.607281    5
2013-04-15 10:26:59.626514    6
2013-04-15 10:26:59.771110    3
Warning message:
timezone of object (EST) is different than current timezone ().

I need to generate time series entries every 100 milliseconds carrying the last value for that period. 我需要每100毫秒生成一个时间序列条目,其中包含该时间段的最后一个值。 For example: 例如:

                           [,1]
2013-04-15 10:26:58.000000    4
2013-04-15 10:26:59.100000    4
2013-04-15 10:26:59.200000    1
2013-04-15 10:26:59.300000    10
2013-04-15 10:26:59.400000    8
...

Note how the last entry carries 8, that is the last entry for the .300000 to .399999 period. 注意最后一个条目如何携带8,即.300000到.399999期间的最后一个条目。

I'm not sure if this will work on Windows, since support for sub-second accuracy is poor, but this works on Ubuntu. 我不确定这是否适用于Windows,因为支持亚秒级精度很差,但这适用于Ubuntu。

library(xts)
options(digits.secs=6)
set.seed(1234)
x <-  xts(1:10, as.POSIXlt(1366039619, tz="EST", origin="1970-01-01")
  + rnorm(10, 500000, 250000)/1000000)
ti <- trunc(index(x))
ms <- rep(seq(min(ti),max(ti),by="s"), each=10)+0:9/10
a <- merge(x,ms,fill=na.locf)[ms]

You'll notice that you handle this the same as any other instance where you need to create a regular xts series from irregular data. 你会注意到你处理它与你需要从不规则数据创建常规xts系列的任何其他实例一样。 It's a bit harder though, since it's more difficult to generate a sub-second sequence. 但这有点困难,因为生成亚秒序列更加困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM