Dear R users, I would like to calculate a cumulative sum as following: on the i-th row, in the j-th column of a matrix, I would like to obtain the sum of the (i-1) preceding elements of this j-th column. Could I avoid a "for" loop? Thank you, Tristan Lorino ---------------------- ?cole Nationale V?t?rinaire d'Alfort - http://www.vet-alfort.fr, T?l. 01 43 96 70 33 M?l. tlorino at vet-alfort.fr Site personnel : http://daedale.free.fr -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Please check the ?cumsum. cumsum[x[,,j])[i] Kind Regards, Shawn Lee tlorino at vet-alfort.fr wrote:> Dear R users, > > I would like to calculate a cumulative sum as following: on the i-th row, in the > j-th column of a matrix, I would like to obtain the sum of the (i-1) preceding elements of > this j-th column. > Could I avoid a "for" loop? > > Thank you, > Tristan Lorino > > ---------------------- > ?cole Nationale V?t?rinaire d'Alfort - http://www.vet-alfort.fr, > T?l. 01 43 96 70 33 > M?l. tlorino at vet-alfort.fr > Site personnel : http://daedale.free.fr > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-------------- next part -------------- A non-text attachment was scrubbed... Name: Shawn.Lee.vcf Type: text/x-vcard Size: 339 bytes Desc: Card for Shawn Lee Url : https://stat.ethz.ch/pipermail/r-help/attachments/20020708/96da6271/Shawn.Lee.vcf
On 07/08/02 09:47, tlorino at vet-alfort.fr wrote:>Dear R users, > >I would like to calculate a cumulative sum as following: on the i-th row, in the >j-th column of a matrix, I would like to obtain the sum of the (i-1) preceding elements of >this j-th column. >Could I avoid a "for" loop?If the matrix is m1, then apply(m1,1,cumsum) does it, but the result it transformed, so if you want to put it back so that rows in the new matrix correspond to rows in the old matrix: t(apply(m1,1,cumsum)) -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron R page: http://finzi.psych.upenn.edu/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi Tristan I think the following solves most of your problem Kind regards, Anders.> x<-matrix(1:9,3) > apply(x,2,cumsum)[,1] [,2] [,3] [1,] 1 4 7 [2,] 3 9 15 [3,] 6 15 24 On Mon, 2002-07-08 at 09:47, tlorino at vet-alfort.fr wrote:> Dear R users, > > I would like to calculate a cumulative sum as following: on the i-th row, in the > j-th column of a matrix, I would like to obtain the sum of the (i-1) preceding elements of > this j-th column. > Could I avoid a "for" loop? > > Thank you, > Tristan Lorino > > ---------------------- > ?cole Nationale V?t?rinaire d'Alfort - http://www.vet-alfort.fr, > T?l. 01 43 96 70 33 > M?l. tlorino at vet-alfort.fr > Site personnel : http://daedale.free.fr > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Did you try 'cumsum' ? Hopin' it helps, L. On Mon, Jul 08, 2002 at 09:47:10AM +0200, tlorino at vet-alfort.fr wrote:> Dear R users, > > I would like to calculate a cumulative sum as following: on the i-th row, in the > j-th column of a matrix, I would like to obtain the sum of the (i-1) preceding elements of > this j-th column. > Could I avoid a "for" loop? > > Thank you, > Tristan Lorino > > ---------------------- > ?cole Nationale V?t?rinaire d'Alfort - http://www.vet-alfort.fr, > T?l. 01 43 96 70 33 > M?l. tlorino at vet-alfort.fr > Site personnel : http://daedale.free.fr > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- -------------------------------------------------------------- Laurent Gautier CBS, Building 208, DTU PhD. Student DK-2800 Lyngby,Denmark tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._