[R] How to get a running mean result by R?
Rui Barradas
ruipbarradas at sapo.pt
Fri Jun 14 10:38:38 CEST 2013
Hello,
As for Inf, the mean value of Inf and anything is Inf, so there's no way
to solve it. As for NaN, you can set them to NA prior to calling the
function.
That leaves us with NA handling. forecast::ma handles NAs, it propagates
them, as it should. An alternative function using filter() like it was
proposed would do the same:
ma2 <- function(x, order, sides = 2){
y <- filter(x, rep(1/order, order), method = "convolution", sides = sides)
as.numeric(y)
}
x <- 1:10
x[3] <- NA
ma2(x, 3)
forecast::ma(x, 3)
But if instead of NA the problem value is NaN, I prefer forecast::ma.
x[3] <- NaN
ma2(x, 3)
forecast::ma(x, 3)
Finally, if the value is Inf, both functions work as expected.
x <- 1:10
x[3] <- Inf
ma2(x, 3)
forecast::ma(x, 3)
So I would say that forecast::ma does handle the three cases.
Hope this helps,
Rui Barradas
Em 14-06-2013 02:50, Jie Tang escreveu:
> yes ,Ma in forecast package does works but when the data included NA,NAN
> or INF ,it could not go on calculating the running
>
>
> 2013/6/12 Rui Barradas <ruipbarradas at sapo.pt <mailto:ruipbarradas at sapo.pt>>
>
> Hello,
>
> You can use, for instance, function ma() in package forecast.
>
> # if not yet installed
> #install.packages('forecast', dependencies = TRUE)
> library(forecast)
>
> ?ma
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Em 12-06-2013 08:21, Jie Tang escreveu:
>
> Hi R users:
> I have a big data and want to calculate the running mean of
> this data .
> How can I get this kind of result ? I have check the command "mean"
> it seems "mean" could not get the running mean?
>
>
>
>
> --
> TANG Jie
> Email: totangjie at gmail.com <mailto:totangjie at gmail.com>
> Tel: 0086-2154896104
> Shanghai Typhoon Institute,China
More information about the R-help
mailing list