[R] fast rowCumsums wanted for calculating the cdf

Joshua Wiley jwiley.psych at gmail.com
Fri Oct 15 10:17:03 CEST 2010


On Fri, Oct 15, 2010 at 12:23 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
>
> Hi,
>
> You might look at Reduce().  It seems faster.  I converted the matrix
> to a list in an incredibly sloppy way (which you should not emulate)
> because I cannot think of  the simple way.

Dennis provided the answer:  system.time(add(unclass(as.data.frame(probs))))

I probably could have suggested Reduce days ago if I was anywhere near
that efficient in my coding....

>
> > probs <- t(matrix(rep(1:10000000), nrow=10)) # matrix with row-wise probabilites
> > F <- matrix(0, nrow=nrow(probs), ncol=ncol(probs));
> > F[,1] <- probs[,1,drop=TRUE];
> > add <- function(x) {Reduce(`+`, x, accumulate = TRUE)}
> >
> >
> > system.time(F.slow <- t(apply(probs, 1, cumsum)))
>   user  system elapsed
>  36.758   0.416  42.464
> >
> > system.time(for (cc in 2:ncol(F)) {
> +  F[,cc] <- F[,cc-1,drop=TRUE] + probs[,cc,drop=TRUE];
> + })
>   user  system elapsed
>  0.980   0.196   1.328
> >
> > system.time(add(list(probs[,1], probs[,2], probs[,3], probs[,4], probs[,5], probs[,6], probs[,7], probs[,8], probs[,9], probs[,10])))
>   user  system elapsed
>  0.420   0.072   0.539

Josh



More information about the R-help mailing list