[R] running mean in blocks

Joshua Wiley jwiley.psych at gmail.com
Mon Jul 11 15:23:31 CEST 2011


Hi,

Take a look at package "zoo", perhaps something like:

require(zoo)
x <- rnorm(10000) # hypothetical data
m <- rollapply(zoo(x), width = 100, FUN = mean, by = 100)

Another (bit more of a hack) option would be:

## convert vector to matrix byrows with 100 columns per row
## (where 100 is the number of points you want at a time)
## use rowMeans function to get mean of each row quickly
m2 <- rowMeans(matrix(x, ncol = 100, byrow = TRUE))


?rollapply # from package zoo
?rowMeans # in base

HTH,

Josh

On Mon, Jul 11, 2011 at 2:23 AM, M. B <workstuffr at googlemail.com> wrote:
> Hello,
> I am running R on Linux and have a column of data 10000 points long. I would
> like to take a moving average of say 100 points at a time, so I end up with
> one column of 100 points. What is the simplest way of doing this?  I would
> like to be able to adjust the width of the averaging blocks.
>
> Thanks much!
>
>        [[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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list