[R] Computing Rolling Average

David Winsemius dwinsemius at comcast.net
Tue Nov 16 21:12:39 CET 2010


On Nov 16, 2010, at 2:33 PM, 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.

There is a rollmean function in package zoo. See below.

>
>
>
> 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

I think you got your first ones out of sequence:

 > apply(a, 2, rollmean, k=7)
       [,1] [,2]
  [1,]    4   21
  [2,]    5   22
  [3,]    6   23
  [4,]    7   24
  [5,]    8   25
  [6,]    9   26
  [7,]   10   27
  [8,]   11   28
  [9,]   12   29
[10,]   13   30
[11,]   14   31


-- 
David.
> [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,
>
> Paolo
>
>
>
> On 15 November 2010 20:59, wangwallace <talenttree at gmail.com> wrote:
>
>>
>> Hey,
>>
>> I am hoping someone can help me with a sampling question.
>>
>> I have a data frame of 8 variables (the first column is the  
>> subjects' id):
>>
>>   SubID    CSE1 CSE2 CSE3 CSE4 WSE1 WSE2 WSE3 WSE4
>>     1          6      5       6       2      6      2         
>> 2       4
>>     2          6      4       7       2      6      6         
>> 2       3
>>     3          5      5       5       5      5      5         
>> 4       5
>>     4          5      4       3       4      4      4         
>> 5       2
>>     5          5      6       7       5      6      4         
>> 4       1
>>     6          5      4       3       6      4      3         
>> 7       3
>>     7          3      6       6       3      6      5         
>> 2       1
>>     8          3      6       6       3      6      5         
>> 4       7
>>
>> the 6 variables are categorized into two groups with CSE1, CSE2,  
>> CSE3, and
>> CSE4 in one group and the rest in another group.
>>
>>> sample(data[,2:4],2,replace=FALSE)
>>
>>  CSE1 CSE2
>> 1      6    5
>> 2      6    4
>> 3      5    5
>> 4      5    4
>> 5      5    6
>> 6      5    4
>> 7      3    6
>> 8      3    6
>>
>> Now I want to sample 1 column from another group of variables  
>> (i.e., WSE1,
>> WSE2, WSE3, WSE4), but I want to restrict a vector I am going to  
>> sample
>> from
>> to only those columns that are not correspond to GROUP 1 variables  
>> I have
>> sampled. That is, I want to sample a column from WSE3, WSE4  Columns
>> corresponding to CSE1 and CSE2 (i.e., WSE1, WSE2) need to be dropped.
>>
>> How can I do this? what if I want to repeat this whole process  
>> (drawing 2
>> random columns from CSE1, CSE2, CSE3, and CSE4 first, AND then  
>> another
>> random column from WSE1, WSE2, WSE3, and WSE4) for 1000 times. any  
>> ideas?
>>
>> Many thanks in advance!!
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3043804.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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<http://www.r-project.org/posting-guide.html 
>> >
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list