Reputation: 2949
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
Upvotes: 1
Views: 1103
Reputation: 301
Package data.table
offers a rolling function, useful for single- as well as multiple time series:
head(
as.data.table(ob)[, roll_power := frollmean(power, 4, align = 'right')]
)
# at the end of a 4 1/2 hour lag
index power roll_power
1: 2015-09-01 00:00:00 1 NA
2: 2015-09-01 00:30:00 2 NA
3: 2015-09-01 01:00:00 3 NA
4: 2015-09-01 01:30:00 4 2.5 # the rolling mean covers this, and preceding rows
5: 2015-09-01 02:00:00 5 3.5
6: 2015-09-01 02:30:00 6 4.5
Upvotes: 0
Reputation: 531
Based on the update to the question "Expected output" and the comment by R.S.:
library(TTR)
head(SMA(ob$power, 4)) # 2 hour moving average
result
SMA
2015-09-01 00:00:00 NA
2015-09-01 00:30:00 NA
2015-09-01 01:00:00 NA
2015-09-01 01:30:00 2.5
2015-09-01 02:00:00 3.5
2015-09-01 02:30:00 4.5
This assumes the 30 minute interval stated in question.
To look more exactly like Expected Output:
lag(head(SMA(ob$power, 4),7))
SMA
2015-09-01 00:00:00 NA
2015-09-01 00:30:00 NA
2015-09-01 01:00:00 NA
2015-09-01 01:30:00 NA
2015-09-01 02:00:00 2.5
2015-09-01 02:30:00 3.5
2015-09-01 03:00:00 4.5
Upvotes: 0
Reputation: 176718
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)
Upvotes: 2
Reputation: 24198
We could use rollapply()
package in combination with lag()
to offset the resulting rolling mean
by one row.
rollapply(lag(ob), 4, mean)
# power
#2015-09-01 00:00:00 NA
#2015-09-01 00:30:00 NA
#2015-09-01 01:00:00 NA
#2015-09-01 01:30:00 NA
#2015-09-01 02:00:00 2.5
#2015-09-01 02:30:00 3.5
#2015-09-01 03:00:00 4.5
# Or if you want it as new variable in your xts object
ob$mean <- rollapply(lag(ob),4,mean)
Upvotes: 1