Search code examples
rtime-seriesxts

Subset xts time-series object in R


I have time-series xts object for certain months like this

library(xts)
  seq<- seq(as.POSIXct("2015-09-01"),as.POSIXct("2015-09-04"), by = "30 mins")
  ob<- xts(data.frame(power=1:(length(seq))),seq)

Now, corresponding to each observation (say A) I want to calculate mean of the last two hours observations. Therefore, corresponding to each observation (A) I need to calculate index of the observation happened before two hours to A, say it is B. Then I can calculate mean of the observations between A and B. Accordingly,

i=10 # dummy
ind_cur<- index(ob[i,]) # index of current observation
ind_back <- ind_cur - 3600 * 2 # index of 2 hours back observation

With these indices, I am subsetting ob as

 ob['ind_cur/ind_back']

It results in following error:

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year,  : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In as_numeric(YYYY) : NAs introduced by coercion
2: In as_numeric(MM) : NAs introduced by coercion
3: In as_numeric(DD) : NAs introduced by coercion
4: In as_numeric(YYYY) : NAs introduced by coercion
5: In as_numeric(MM) : NAs introduced by coercion
6: In as_numeric(DD) : NAs introduced by coercion

Can anyone help me to subset ob! Found a related question at the link, but not enough to solve this issue.

Update Expected output shown as

2015-09-01 00:00:00     1   NA # as I don't have previous data
2015-09-01 00:30:00     2   NA
2015-09-01 01:00:00     3   NA
2015-09-01 01:30:00     4   NA
2015-09-01 02:00:00     5   10/4 # mean of prevous 4 observations (last two hours)
2015-09-01 02:30:00     6   14/4  
2015-09-01 03:00:00     7   18/4

Solution

  • This is a difficult problem to solve generally, so you need to roll your own solution. The easiest is to use window to subset by overlapping 2-hour intervals.

    # initialize a result object
    ob2 <- ob * NA_real_
    # loop over all rows and calculate 2-hour mean
    for(i in 2:nrow(ob)) {
      ix <- index(ob)[i]
      ob2[i] <- mean(window(ob, start=ix-3600*2, end=ix))
    }
    # set incomplete 2-hour intervals to NA
    is.na(ob2) <- which(index(ob2) < start(ob2)+3600*2)