Skip to content
Prev 336724 / 398513 Next

How to calculate moving average without using filter()?

On Feb 17, 2014, at 10:45 AM, C W wrote:

            
Construct a vector for grouping and use tapply. Modulo division is a common method for achieving this. Sometimes the seq-function can be used if you adjust the length properly.
0    1    2    3    4    5    6 
 2.0  5.0  8.0 11.0 14.0 17.0 19.5 


tapply(dat, round(seq(1, (length(dat)/3),  len=length(dat))), mean)
   1    2    3    4    5    6    7 
 1.5  4.5  8.0 11.0 14.5 18.0 20.0 

The comment about weighting dos not seem to be exemplified in your example.
David Winsemius
Alameda, CA, USA