rollapply in the zoo package can do that:
> library(zoo)
> x <- zoo(1:10)
> x[5] <- NA
> rollapply(x, 3, mean, na.rm = TRUE)
2 3 4 5 6 7 8 9
2.0 3.0 3.5 5.0 6.5 7.0 8.0 9.0
> xm <- rollapply(x, 3, mean, na.rm = TRUE)
> xm
2 3 4 5 6 7 8 9
2.0 3.0 3.5 5.0 6.5 7.0 8.0
The S-plus function moving.ave(data, span = 2) calculates the moving
average, but it does not have an argument to tell it how to deal with
NA values, so it will return NA for all averages as shown below.
Is there an R or S moving average function which is able to omit some
NA values in the dataset
2 matches
Mail list logo