Paolo Rossi
2010-Nov-17 19:27 UTC
[R] Parameterising apply To Compute Rolling Average of Columns in a matrix
I sent a post to find a clever way to compute a Rolling Average of columns in a matrix and I was given the solution below which I am very pleased with. RollingAverage <- function(x, RollingObs) { cx <- cumsum(x); N <- length(x); Temp <- (cx[RollingObs:N] - c(0, cx[1:(N-RollingObs)]))/RollingObs Output <- array(NA, N) Output[RollingObs:N] <- Temp; Output } The only drawback is that in order to use apply to iterate over columns one needs to write a fucntion for any RollingObs unless one is able to pass a parameter through an apply call. Example: a= array(1:100, dim = c(17,2)) RollingAverage(a[, 1], 7) # works fine apply(a, 2, RollingAverage) # this doesnt work as it doesnt see RollingObs Is there a way to pass an additional parameter to apply? Thanks Paolo On 16 November 2010 20:05, Ray Brownrigg <Ray.Brownrigg@ecs.vuw.ac.nz>wrote:> On Wed, 17 Nov 2010, Paolo Rossi wrote: > > Hi, > > Can anyone suggest a clever way to compute a rolling weekly average of > the > > columns in a matrix? The column bit is straightforward – use apply given > a > > function which does what you want on a column. With regard to a > particular > > column, the obvious way is to run a for loop indexing the last 7 days and > > computing the average . I simply would like to know if there is a better > / > > quicker way. > > > > > > > > Code: > > Given a, > > > > > a= array(1:100, dim = c(17,2)) > > > a > > > > [,1] [,2] > > [1,] 1 18 > > [2,] 2 19 > > [3,] 3 20 > > [4,] 4 21 > > [5,] 5 22 > > [6,] 6 23 > > [7,] 7 24 > > [8,] 8 25 > > [9,] 9 26 > > [10,] 10 27 > > [11,] 11 28 > > [12,] 12 29 > > [13,] 13 30 > > [14,] 14 31 > > [15,] 15 32 > > [16,] 16 33 > > [17,] 17 34 > > one needs to start computing the average from obs 7 s (at obs 7 you have > a > > full week to compute the average) and compute the rolling weekly average > > from day 7 onwards > > Results will look like b > > [,1] [,2] > > [1,] 4 14 > > [2,] 5 21 > > [3,] 6 22 > > [4,] 7 23 > > [5,] 8 24 > > [6,] 9 25 > > [7,] 10 26 > > [8,] 11 27 > > [9,] 12 28 > > [10,] 13 29 > > Thanks in advance, > > > I don't see how an average of 7 numbers all 18 or greater can be 14, as in > your > result[1, 2], unless you have mis-stated the question. > > Anyway, try: > apply(a, 2, function(x) {cx <- cumsum(x); N <- length(x); (cx[7:N] - c(0, > cx[1:(N-7)]))/7} > > HTH > Ray Brownrigg > > > Paolo >[[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Nov-17 19:32 UTC
[R] Parameterising apply To Compute Rolling Average of Columns in a matrix
You've tried? apply(a, 2, RollingAverage, 7) On Wed, Nov 17, 2010 at 5:27 PM, Paolo Rossi < statmailinglists@googlemail.com> wrote:> I sent a post to find a clever way to compute a Rolling Average of columns > in a matrix and I was given the solution below which I am very pleased > with. > > > RollingAverage <- function(x, RollingObs) { > cx <- cumsum(x); > N <- length(x); > Temp <- (cx[RollingObs:N] - c(0, cx[1:(N-RollingObs)]))/RollingObs > Output <- array(NA, N) > Output[RollingObs:N] <- Temp; > Output > } > > > The only drawback is that in order to use apply to iterate over columns one > needs to write a fucntion for any RollingObs unless one is able to pass a > parameter through an apply call. > > Example: > a= array(1:100, dim = c(17,2)) > RollingAverage(a[, 1], 7) # works fine > apply(a, 2, RollingAverage) # this doesnt work as it doesnt see > RollingObs > > Is there a way to pass an additional parameter to apply? > > Thanks > > Paolo > > > > > > > On 16 November 2010 20:05, Ray Brownrigg <Ray.Brownrigg@ecs.vuw.ac.nz > >wrote: > > > On Wed, 17 Nov 2010, Paolo Rossi wrote: > > > Hi, > > > Can anyone suggest a clever way to compute a rolling weekly average of > > the > > > columns in a matrix? The column bit is straightforward – use apply > given > > a > > > function which does what you want on a column. With regard to a > > particular > > > column, the obvious way is to run a for loop indexing the last 7 days > and > > > computing the average . I simply would like to know if there is a > better > > / > > > quicker way. > > > > > > > > > > > > Code: > > > Given a, > > > > > > > a= array(1:100, dim = c(17,2)) > > > > a > > > > > > [,1] [,2] > > > [1,] 1 18 > > > [2,] 2 19 > > > [3,] 3 20 > > > [4,] 4 21 > > > [5,] 5 22 > > > [6,] 6 23 > > > [7,] 7 24 > > > [8,] 8 25 > > > [9,] 9 26 > > > [10,] 10 27 > > > [11,] 11 28 > > > [12,] 12 29 > > > [13,] 13 30 > > > [14,] 14 31 > > > [15,] 15 32 > > > [16,] 16 33 > > > [17,] 17 34 > > > one needs to start computing the average from obs 7 s (at obs 7 you > have > > a > > > full week to compute the average) and compute the rolling weekly > average > > > from day 7 onwards > > > Results will look like b > > > [,1] [,2] > > > [1,] 4 14 > > > [2,] 5 21 > > > [3,] 6 22 > > > [4,] 7 23 > > > [5,] 8 24 > > > [6,] 9 25 > > > [7,] 10 26 > > > [8,] 11 27 > > > [9,] 12 28 > > > [10,] 13 29 > > > Thanks in advance, > > > > > I don't see how an average of 7 numbers all 18 or greater can be 14, as > in > > your > > result[1, 2], unless you have mis-stated the question. > > > > Anyway, try: > > apply(a, 2, function(x) {cx <- cumsum(x); N <- length(x); (cx[7:N] - c(0, > > cx[1:(N-7)]))/7} > > > > HTH > > Ray Brownrigg > > > > > Paolo > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Ray Brownrigg
2010-Nov-17 19:44 UTC
[R] Parameterising apply To Compute Rolling Average of Columns in a matrix
On Thu, 18 Nov 2010, Paolo Rossi wrote:> I sent a post to find a clever way to compute a Rolling Average of columns > in a matrix and I was given the solution below which I am very pleased > with. > > > RollingAverage <- function(x, RollingObs) { > cx <- cumsum(x); > N <- length(x); > Temp <- (cx[RollingObs:N] - c(0, cx[1:(N-RollingObs)]))/RollingObs > Output <- array(NA, N) > Output[RollingObs:N] <- Temp; > Output > } > > > The only drawback is that in order to use apply to iterate over columns one > needs to write a fucntion for any RollingObs unless one is able to pass a > parameter through an apply call. > > Example: > a= array(1:100, dim = c(17,2)) > RollingAverage(a[, 1], 7) # works fine > apply(a, 2, RollingAverage) # this doesnt work as it doesnt see > RollingObs > > Is there a way to pass an additional parameter to apply? >RTFM. Thats what the ... option of apply() is for. apply(a, 2, RollingAverage, RollingObs=7) Ray> Thanks > > Paolo > > On 16 November 2010 20:05, Ray Brownrigg <Ray.Brownrigg at ecs.vuw.ac.nz>wrote: > > On Wed, 17 Nov 2010, Paolo Rossi wrote: > > > Hi, > > > Can anyone suggest a clever way to compute a rolling weekly average of > > > > the > > > > > columns in a matrix? The column bit is straightforward ? use apply > > > given > > > > a > > > > > function which does what you want on a column. With regard to a > > > > particular > > > > > column, the obvious way is to run a for loop indexing the last 7 days > > > and computing the average . I simply would like to know if there is a > > > better > > > > / > > > > > quicker way. > > > > > > > > > > > > Code: > > > Given a, > > > > > > > a= array(1:100, dim = c(17,2)) > > > > a > > > > > > [,1] [,2] > > > [1,] 1 18 > > > [2,] 2 19 > > > [3,] 3 20 > > > [4,] 4 21 > > > [5,] 5 22 > > > [6,] 6 23 > > > [7,] 7 24 > > > [8,] 8 25 > > > [9,] 9 26 > > > [10,] 10 27 > > > [11,] 11 28 > > > [12,] 12 29 > > > [13,] 13 30 > > > [14,] 14 31 > > > [15,] 15 32 > > > [16,] 16 33 > > > [17,] 17 34 > > > one needs to start computing the average from obs 7 s (at obs 7 you > > > have > > > > a > > > > > full week to compute the average) and compute the rolling weekly > > > average from day 7 onwards > > > Results will look like b > > > [,1] [,2] > > > [1,] 4 14 > > > [2,] 5 21 > > > [3,] 6 22 > > > [4,] 7 23 > > > [5,] 8 24 > > > [6,] 9 25 > > > [7,] 10 26 > > > [8,] 11 27 > > > [9,] 12 28 > > > [10,] 13 29 > > > Thanks in advance, > > > > I don't see how an average of 7 numbers all 18 or greater can be 14, as > > in your > > result[1, 2], unless you have mis-stated the question. > > > > Anyway, try: > > apply(a, 2, function(x) {cx <- cumsum(x); N <- length(x); (cx[7:N] - c(0, > > cx[1:(N-7)]))/7} > > > > HTH > > Ray Brownrigg > > > > > Paolo
Gabor Grothendieck
2010-Nov-28 00:39 UTC
[R] Parameterising apply To Compute Rolling Average of Columns in a matrix
On Wed, Nov 17, 2010 at 2:27 PM, Paolo Rossi <statmailinglists at googlemail.com> wrote:> I sent a post to find a clever way to compute a Rolling Average of columns > in a matrix ?and I was given the solution below which I am very pleased > with. >There is a zoo function that does this. The following converts a to zoo, performs the rolling average on columns and then converts back to a matrix. (A zoo object is just a matrix with a time index.) library(zoo) coredata(rollmean(as.zoo(a), 7)) Depending on what this is part of, you might be better off to keep it as a zoo object since then you have access to all the other facilities as well. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com