Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate regular xts periods from random time observations?

Tags:

r

xts

I have the following xts matrix:

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

like image 477
Robert Kubrick Avatar asked Apr 23 '13 21:04

Robert Kubrick


1 Answers

I'm not sure if this will work on Windows, since support for sub-second accuracy is poor, but this works on 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. It's a bit harder though, since it's more difficult to generate a sub-second sequence.

like image 164
Joshua Ulrich Avatar answered Oct 19 '22 19:10

Joshua Ulrich