Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving windows down through a vector in R [duplicate]

I'd like to take a vector of numerics, then using a specified window width & stride I want to run down through the vector capturing the means of each window by their stride. I can code this but I'm wondering if there's a neater way with a package etc..?

enter image description here

like image 942
PaulBeales Avatar asked Feb 16 '26 21:02

PaulBeales


1 Answers

One way to achieve what you need is by using rollapply from zoo package.

library(zoo)
rollapply(x, width = 7, by = 4, mean)
#[1] 622.1429 682.8571 540.7143

DATA

x <-c(267, 497, 836, 498, 923, 836, 498, 923, 267, 497, 836, 498, 923, 267, 497)
like image 172
Sotos Avatar answered Feb 19 '26 11:02

Sotos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!