[R] apply and arrays

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jul 25 08:58:53 CEST 2005


On Mon, 25 Jul 2005, Uwe Ligges wrote:

> Laura Holt wrote:
>
>> Hi R!
>>
>> I have a 3 dimensional array, which is 21 x 3 x 3
>>
>> I want to use apply to sum on each 21x3 matrix, which is fine.
>>
>> Is there a way that I can do this in 1 step instead of a loop (3), please?
>
> Don't know which direction you mean, I guess one of the following:
>
> apply(x, c(1,2), sum)
> apply(x, 3, sum)

Or use rowSums or colSums for clarity (and speed, irrelevant here).

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list