[R] using "rollapply" to calculate a moving sum or running sum?
William Dunlap
wdunlap at tibco.com
Sat Aug 3 19:34:04 CEST 2013
E.g.,
> mymatrix <- matrix(data=sin(1:(45000*400)),nrow=45000, ncol=400)
> system.time(z1 <- rollapply(t(mymatrix),width=12,FUN=sum,by.column=TRUE,fill=NA,partial=FALSE))
user system elapsed
1592.85 1311.56 2905.99
> system.time(z2 <- filter(t(mymatrix), rep(1,12)))
user system elapsed
5.17 0.16 5.32
> all.equal(array(z1, dim=dim(z1)), array(z2, dim=dim(z2)), tol=1e-15)
[1] TRUE
(Both filter() and rollapply() add irrelevant attributes to their outputs so I remove
them with the call to matrix() in all.equal().)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: William Dunlap
> Sent: Friday, August 02, 2013 2:57 PM
> To: 'Anika Masters'; arun
> Cc: R help
> Subject: RE: [R] using "rollapply" to calculate a moving sum or running sum?
>
> Try the filter() function.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> > Of Anika Masters
> > Sent: Friday, August 02, 2013 2:48 PM
> > To: arun
> > Cc: R help
> > Subject: Re: [R] using "rollapply" to calculate a moving sum or running sum?
> >
> > 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()
> >
> > On Thu, Jun 27, 2013 at 2:41 PM, arun <smartpink111 at yahoo.com> wrote:
> > > Hi,
> > > Try:
> > > library(zoo)
> > >
> > >
> >
> rollapply(t(mymatrix),width=12,FUN=sum,by.column=T,fill=NA,partial=FALSE,align="left"
> > )
> > > # [,1] [,2] [,3] [,4] [,5]
> > > #[1,] 342 354 366 378 390
> > > #[2,] 402 414 426 438 450
> > > #[3,] 462 474 486 498 510
> > > #[4,] 522 534 546 558 570
> > > #[5,] 582 594 606 618 630
> > > #[6,] 642 654 666 678 690
> > > #[7,] 702 714 726 738 750
> > > #[8,] 762 774 786 798 810
> > > #[9,] 822 834 846 858 870
> > > #[10,] NA NA NA NA NA
> > > #[11,] NA NA NA NA NA
> > > #[12,] NA NA NA NA NA
> > > #[13,] NA NA NA NA NA
> > > #[14,] NA NA NA NA NA
> > > #[15,] NA NA NA NA NA
> > > #[16,] NA NA NA NA NA
> > > #[17,] NA NA NA NA NA
> > > #[18,] NA NA NA NA NA
> > > #[19,] NA NA NA NA NA
> > > #[20,] NA NA NA NA NA
> > > A.K.
> > >
> > >
> > >
> > > ----- Original Message -----
> > > From: Anika Masters <anika.masters at gmail.com>
> > > To: R help <r-help at r-project.org>
> > > Cc:
> > > Sent: Thursday, June 27, 2013 3:00 PM
> > > Subject: [R] using "rollapply" to calculate a moving sum or running sum?
> > >
> > > #using "rollapply" to calculate a moving sum or running sum?
> > >
> > > #I am tryign to use rollapply to calcualte a moving sum? #I tried
> > > rollapply and get the error message
> > > #"Error in seq.default(start.at, NROW(data), by = by) :
> > > # wrong sign in 'by' argument"
> > >
> > > #example:
> > >
> > > mymatrix <- ( matrix(data=1:100, nrow=5, ncol=20) )
> > > mymatrix_cumsum <- ( matrix(data=NA, nrow=5, ncol=20) )
> > > w=12
> > > for(i in 1: (ncol(mymatrix)-w+1) ) {
> > > mymatrix_cumsum[ , i] <- apply(X=mymatrix[, i:(i+w-1)] , MARGIN=1,
> > > FUN=sum, na.rm=T)
> > > }
> > >
> > > #How might I use the "rollapply" function instead?
> > >
> > > rollapply(mymatrix, 12, sum)
> > >
> > > rollapply(data = mymatrix, width = 12, FUN=sum, by.column =T, fill =
> > > NA, partial = FALSE, align = "left" )
> > >
> > > ______________________________________________
> > > 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.
> > >
> >
> > ______________________________________________
> > 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.
More information about the R-help
mailing list