Skip to content
Prev 65302 / 398502 Next

apply a function to a rolling subset of a vector

On Wed, 2 Mar 2005 17:22:43 -0500, "Whit Armstrong"
<whit at twinfieldscapital.com> wrote :
I don't know of a general purpose function, but filter() (in the stats
package) can do the example you give, or any other linear filter.

e.g.

x <- rnorm(1000)
y <- filter(x, rep(1,20))

puts 20 element sums into y.  The vector ends up the same length as x,
with NAs at the beginning and end (by default).

Duncan Murdoch