Re: [R] summing array elements

From: Erich Neuwirth <erich.neuwirth_at_univie.ac.at>
Date: Fri, 27 May 2011 01:18:17 +0200

> 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.
>



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. Received on Thu 26 May 2011 - 23:22:55 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 30 May 2011 - 09:40:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive