[R] using "rollapply" to calculate a moving sum or running sum?

Gabor Grothendieck ggrothendieck at gmail.com
Sat Aug 3 14:27:23 CEST 2013


On Sat, Aug 3, 2013 at 7:16 AM, Enrico Schumann <es at enricoschumann.net> wrote:
> On Fri, 02 Aug 2013, Anika Masters <anika.masters at gmail.com> writes:
>
>> This is not critical, but I am curious to learn. Are there any
>> suggestions for speeding up the process to calculate a moving row sum?
>> (Ideally from within R, as opposed to suing C, etc.)
>> Using rollapply on a matrix of 45,000 rows and 400 columns takes 83 minutes.
>>
>> date()
>> mymatrix <- matrix(data=1:45000, nrow=45000, ncol=400)
>> temp <- t(rollapply(t(mymatrix), width=12, FUN=sum, by.column=T,
>> fill=NA, partial=FALSE, align="left"))
>> date()
>>
>
> Write a function that *quickly* computes the moving sum of a single row;
> then loop over the rows.
>
> Here is such a function, which is a slightly modified copy of the
> function MA ("moving average") in the NMOF package.
>
>   rsum <- function (y, order, pad = NULL) {
>       n <- length(y)
>       ma <- cumsum(y)
>       ma[order:n] <- ma[order:n] - c(0, ma[1L:(n - order)])
>       if (!is.null(pad) && order > 1L)
>           ma[1L:(order - 1L)] <- pad
>       ma
>   }
>
> The main 'trick' is the use of 'cumsum'.  Some speed comparions can
> given here

That is the same trick that rollsum in zoo uses.  There is some
overhead in zoo due to things like converting it to a zoo object
internally but its basically the same alogorithm.



More information about the R-help mailing list