[R] summing array elements

Thomas Lumley tlumley at uw.edu
Fri May 27 00:48:39 CEST 2011


On Fri, May 27, 2011 at 10:06 AM, marco milella <vrukol at gmail.com> 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.

Tell apply() which dimensions you want to keep, in this case 3 and 4

R> m<-array(1, dim=c(2,4,3,3))
R> apply(m,3:4,sum)
     [,1] [,2] [,3]
[1,]    8    8    8
[2,]    8    8    8
[3,]    8    8    8

    -thomas

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland



More information about the R-help mailing list