Question

Is there a better way to achieve for the following code:

slice.periods <- function (x, periods, ...)
{
  if (!require("xts")) {
    stop("Need 'xts'")
  }
  Reduce(rbind.xts, lapply(periods, function(t) x[t], ...))
}

where x is an xts object, periods is a iterable charters list that is recognizable by xts subsetting. Sample Usage:

j <- xts(rnorm(10e6),Sys.time()-(10e6:1))
v <- c("T10:00/T11:00", "T13:00/T15:00", "T20:30/T22:00")
system.time(slice.periods(j, v))

## result on my MacBook Air (1.8 GHz Intel Core i7; 4 GB 1333 MHz DDR3)
##  user  system elapsed 
## 14.956   0.876  15.837 

There are several concerns:

  1. "Reduce" maybe too slow if each time subset is very large
  2. Each Time slice are not optimal as it's not directly using indexing from xts object. If the object is large, it can be time costly.

I saw some posting that if the time is in UTC, there are some amazing speed up by accessing directly, see the following post: data.table time subset vs xts time subset

However, my application requires local timezone with daylight saving. This make different UTC hours conversion between summer and winter and the above approach would not work.

I have also consider using data.table as I have some great performance using "rbindlist" in replacing do.Call(rbind, ...) or Reduce(rbind, ...). As well, data.table has some cool subsetting features, which I am not familiar with. On the other hand, rbindlist and as.data.table will not take xts objects as inputs, and I am not sure using data.table for time-series data subset is a good choice.

If there are other idea, I am open to try out. Thanks many in advance.

Was it helpful?

Solution

This solution will be faster if the bottleneck was rbind.xts, but the bottleneck is the time-of-day subsetting.

jv <- j[unlist(lapply(v, function(i) j[i, which.i=TRUE])),]

Time-of-day subsetting in a non-UTC timezone is slow because xts currently converts the POSIXct index into POSIXlt to get the day of the year.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top