Question

I want to use moving average function (rollmean) in R. Example data:

x<-seq(1,48,by=1)
y<-c(rep(11,12), rep(12,12))
z<-data.frame(x,y)

I want to calculate moving average of the z[,"x"] with lag of 3 days for each z[,"y"] (i.e. 11 & 12) and filled them to the (new) third column where first two rows of each z[,"y"] are designated by NAs.

Was it helpful?

Solution

Using zoo for example using the data.table package:

library(zoo)
library(data.table)
DT <- data.table(z)
DT[, roll_x := rollmeanr(x, 3, fill = NA), y]

Of course if have some missing values it is better to use rollapplyr :

DT[, roll_x := rollapplyr(x, 3, mean, fill = NA), y]

Another package that is going to be faster is caTools:

library(caTools)
DT[, roll_x := runmean(x, 3, align = 'right', endrule = 'NA'), by = y]

In terms of data frames this works too:

transform(z, roll_x = ave(x, y, FUN = function(x) rollmeanr(x, 3, fill = NA)))

OTHER TIPS

You can use the base function filter.

z[ , moving_average := filter(x, rep(1/3, 3), sides = 1), by = "y"]

Note that a lag of 3 results in two NAs.

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