roll_mean | R Documentation |
Fast rolling means with aligment using C++/Rcpp.
Additional performance gains can be achieved by skipping increment
values between calculations.
roll_mean(x, n = 7, increment = 1, align = "center")
x |
an R numeric vector |
n |
integer window size |
increment |
integer shift to use when sliding the window to the next location |
align |
window alignment, one of |
The window size n
is interpreted as the full window length.
Setting increment
to a value greater than one will result in NA
s for all skipped-over indices.
The align
parameter determines the alignment of the current index within the window. Thus:
align="left" [*------]
will cause the returned vector to have n-1 NA
values at the right end.
align="center" [---*---]
will cause the returned vector to have (n-1)/2 NA
values at either end.
align="right" [------*]
will cause the returned vector to have n-1 NA
values at the left end.
A vector of rolling mean values of the same length as x
.
For align="center"
, the window size is increased by one if necessary to guarantee an odd window size.
x <- rep(1:5,each=10)
plot(x,cex=0.8,pch=17,main="Test of roll_mean alignment with a 5-point window")
points(roll_mean(x,5,1,'left'),cex=1.5,col='blue',type='b')
points(roll_mean(x,5,1,'center'),cex=1.5,col='forestgreen',type='b')
points(roll_mean(x,5,1,'right'),cex=1.5,col='red',type='b')
legend("topleft",
legend=c('data','align=left','align=center','align=right'),
col=c('black','blue','forestgreen','red'),
pch=c(17,1,1,1))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.