Hi: Is this what you want?
> m1=cbind(1:5,1:5,1:5) > apply(m1, 1, cumsum) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 2 4 6 8 10 [3,] 3 6 9 12 15 HTH, Dennis On Wed, Apr 14, 2010 at 5:18 AM, Eleni Rapsomaniki <er...@medschl.cam.ac.uk>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@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.