[R] cumsum in 3d arrays

William Dunlap wdunlap at tibco.com
Mon Nov 28 17:07:14 CET 2011


Does the following do what you want?  You often
need to use aperm() to permute the dimensions of
apply()'s output.

> a <- array(2^(0:23), dim=c(2,3,4))
> aperm(apply(a, 1:2, cumsum), c(2,3,1))
, , 1

     [,1] [,2] [,3]
[1,]    1    4   16
[2,]    2    8   32

, , 2

     [,1] [,2] [,3]
[1,]   65  260 1040
[2,]  130  520 2080

, , 3

     [,1]  [,2]   [,3]
[1,] 4161 16644  66576
[2,] 8322 33288 133152

, , 4

       [,1]    [,2]    [,3]
[1,] 266305 1065220 4260880
[2,] 532610 2130440 8521760


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of zloncaric
> Sent: Saturday, November 26, 2011 6:32 AM
> To: r-help at r-project.org
> Subject: [R] cumsum in 3d arrays
> 
> Hello!
> 
> Is it posible to apply /cumsum()/ along the 3rd dimension of 3D array?
> Something like matrlab function - /cumsum (*A*,dim)/ which returns the
> cumulative sum of the elements along the dimension of *A* specified by
> scalar dim.
> 
> Thanks in advance
> 
> Željka
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/cumsum-in-3d-arrays-tp4110470p4110470.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.


More information about the R-help mailing list