[R] Running cumulative sums in matrices

Eleni Rapsomaniki er339 at medschl.cam.ac.uk
Wed Apr 14 15:08:11 CEST 2010


That's really interesting... I have always assumed that for-loops take
longer than apply. Perhaps it depends on the application. I'll try both
in my code and see. 

Thank you!
Eleni Rapsomaniki
 
Research Associate
Tel:     +44 (0) 1223 740273
Strangeways Research Laboratory
Department of Public Health and Primary Care
University of Cambridge
 

-----Original Message-----
From: Dimitris Rizopoulos [mailto:d.rizopoulos at erasmusmc.nl] 
Sent: 14 April 2010 13:47
To: Eleni Rapsomaniki
Cc: r-help at r-project.org
Subject: Re: [R] Running cumulative sums in matrices

you can even use a simple for-loop, e.g.,

m1 <- cbind(1:5,1:5,1:5)

out <- m1
for(i in 1:nrow(out))
     out[i, ] <- cumsum(out[i, ])

which seems to be faster than apply(m1, 1, cumsum), i.e.,

m1 <- m1[rep(1:5, each = 1e04), ]

library(rbenchmark)
benchmark(
     "apply" = apply(m1, 1, cumsum),
     "for" = {out <- m1; for(i in 1:nrow(out)) out[i, ] <- cumsum(out[i,

])},
     replications = 50, order = "relative"
)


I hope it helps.

Best,
Dimitris


On 4/14/2010 2:18 PM, Eleni Rapsomaniki wrote:
>
> Dear R-helpers,
>
> I have a huge data-set so need to avoid for loops as much as possible.
Can someone think how I can compute the result in the following example
(that uses a for-loop) using some version of apply instead (or any other
similarly super-efficient function)?
>
> example:
> #Suppose a matrix:
> m1=cbind(1:5,1:5,1:5)
>
> #The aim is to create a new matrix with every column containing the
cumulative sum of all previous columns.
> m2=m1
> for(i in 2:ncol(m1)){
>      m2[,i]=apply(m1[,1:i],1,sum)
> }
> m2
>
> Many thanks in advance
>
> Eleni Rapsomaniki
>
> Research Associate
> Strangeways Research Laboratory
> Department of Public Health and Primary Care
> University of Cambridge
>
>
> ______________________________________________
> 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.
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list