Craig Craig - 1 month ago 18
R Question

Sub-second sampling of a XTS object

I have a XTS object with millisecond time-stamps, the differences between indexes are irregular. Is there an easy way of reducing this time-series to 50ms samples, or 1 second samples for that matter?

Answer

Yes, in essence the approach is to

  • create a regular time grid for which you can seq(startPOSIXt, endPOSIXt, by=fracsec) which can be a double, eg 0.1 for 100ms as an "empty" xts (or NA valued xts)
  • merge it with your irregularly sampled observations which will now form a larger data set following the merge with the regular time grid
  • apply na.locf() to your data
  • and then subset from the enlarged set at the regular time points

and presto! you have the most-recent observations at the time points you called for.