7

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.

Robert Kubrick
  • 8,413
  • 13
  • 59
  • 91
  • How did you create the second object? Could you add a `set.seed` call before you call `rnorm`, so your example is fully reproducible? – Joshua Ulrich Apr 23 '13 at 21:31
  • Second object? I did not, it's just an example of the result I need. I'm going to add set.seed() to the first object. – Robert Kubrick Apr 23 '13 at 21:34
  • What if there were multiple observations in one or more periods? – IRTFM Apr 23 '13 at 21:45
  • @DWin Then the last value for that period should be kept (and the older values discarded). That's what I tried to illustrate in the last row of the second matrix. Also if there is no observation for a given period, then the value from the previous period should be carried over to the next period. – Robert Kubrick Apr 23 '13 at 21:47

1 Answers1

8

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.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 1
    I noticed duplicate index entries in the merged set in some cases. I think that's when the same exact index appears in both `x` and `ms` ( same index to millisecond precision). This line would clear the duplicates: `a = a[-(which(c(index(a[-1]),NA) == index(a)))]` – Robert Kubrick Jun 07 '13 at 12:12
  • @Robert-Kubrick I just solved a similar problem to this, and ran into the same problem as this - duplicates. This comment is an important one! Thanks for noting... – FXQuantTrader Jan 04 '14 at 04:15