[R] cumsum

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Sat Nov 3 12:46:52 CET 2007



threshold said the following on 11/3/2007 4:41 AM:
> Hi, my problem belongs to the basic ones. I want to get cumulated sum over
> the matrix columns by one command (if such exists). Ordinary R's cumsum(x)
> when x is:
>      [,1] [,2]
> [1,]    1    5
> [2,]    2    6
> [3,]    3    7
> [4,]    4    8
> 
> yields:  1  3  6 10 15 21 28 36
> I want:
>      [,1] [,2]
> [1,]    1    5
> [2,]    3   11
> [3,]    6   18
> [4,]   10   26
> Is there any command to do so??
> 
> best, robert
> 
> 
> 

Try

apply(x, 2, cumsum)

HTH,

--sundar



More information about the R-help mailing list