[R] summing array elements

Erich Neuwirth erich.neuwirth at univie.ac.at
Fri May 27 01:18:17 CEST 2011


> arr<-1:72
> dim(arr)<-c(2,4,3,3)
> apply(arr,1:2,sum)
     [,1] [,2] [,3] [,4]
[1,]  297  315  333  351
[2,]  306  324  342  360
> apply(arr,3:4,sum)
     [,1] [,2] [,3]
[1,]   36  228  420
[2,]  100  292  484
[3,]  164  356  548
>

apply(arr,1:2,sum)
will sum all elements arr(i1,i2,..)
for each single combination of the first and the second index
over all possible combinations of the third and fourth index.




On 5/27/2011 12:06 AM, marco milella wrote:
> Hi to everybody
> I have an array with dimensions 2,4,3,3.
> Wanting to sum the matrices in the first two dimensions, I'm trying to use
> the "apply" function, but with no results. Have to say I'm quite new with R
> syntax.
> tx in advance
> 
> marco
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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