How to generate regular xts periods from random observations over time?

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 records every 100 milliseconds having the last value for this period. For instance:

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

Notice how the last record contains 8, that is, the last record for a period from 30000 to 0.99999.

+5
source share
1 answer

I'm not sure if this will work on Windows, since support for subgrid accuracy is poor, but it 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 will notice that you handle this just like any other instance where you need to create a regular series of xts from irregular data. This is a bit trickier as it is harder to create a subsequence sequence.

+6
source

All Articles