[R] aggregate arrays

David Winsemius dwinsemius at comcast.net
Fri Feb 3 19:40:15 CET 2012


On Feb 3, 2012, at 11:45 AM, Leuzinger Sebastian wrote:

> Dear list
>
> after quite a bit of research in the archive, I gave up. This seems  
> to be a simple problem:
>
> I would like to aggregate a (3-dimensional) array, either by another  
> array, or by a vector, indicating the dimension which should be  
> aggregated.
>
> I don't think I have to provide an example, it's really the 3- 
> dimensional equivalent for the standard aggregate command. I am sure  
> aggregate() itself can do it, but how do I need to specify the 'by'  
> argument?

Why wouldn't `apply` do what you want?  (Which is perhaps my implicit  
rebuttal to your suggestion that you don't need to produce an example).

 > arr <- array(1:27, c(3,3,3))
 > apply(arr, 2:3, mean)
      [,1] [,2] [,3]
[1,]    2   11   20
[2,]    5   14   23
[3,]    8   17   26
 > apply(arr, 3, mean)
[1]  5 14 23



>
> Any hint (also to an earlier post) appreciated


-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list