Skip to content
Back to formatted view

Raw Message

Message-ID: <53025E08.7020502@sapo.pt>
Date: 2014-02-17T19:07:52Z
From: Rui Barradas
Subject: How to calculate moving average without using filter()?
In-Reply-To: <CAE2FW2kVwgDjKq9r9v38Q5MRZto8kt2Z4Ah1qtzLGNV=iB-oXg@mail.gmail.com>

Hello,

Many packages have a movind average function. For instance package 
forecast. Or

library(sos)
findFn("moving average")

In your example, what you compute is not exactly a moving average, but 
in can be computed with something like the following.

s <- (seq_along(dat) - 1) %/% 3
sapply(split(dat, s), mean)


Hope this helps,

Rui Barradas


Em 17-02-2014 18:45, C W escreveu:
> Hi list,
> How do I calculate a moving average without using filter().  filter() does
> not seem to give weighted averages.
>
> I am looking into apply(), tapply,... But nothing "moves".
>
> For example,
>
> dat<-c(1:20)
> mean(dat[1:3])
> mean(dat[4:6])
> mean(dat[7:9])
> mean(dat[10:12])
>
> etc...
>
> I understand the point of apply is to avoid loops, how should I incorporate
> this idea into using an apply()?
>
> Thanks,
> Mike
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>