[R] mean and array

David Brahm brahm at alum.mit.edu
Wed Jul 3 17:01:32 CEST 2002


Olivier Martin <olivier.martin at inrialpes.fr> wrote:
> I have an array, one say ar, with 3 dimensions.
> dim(ar)
>  >200   3   4
> I would like to calculate the mean on the third dimension.
> mean(ar[1,1,]),mean(ar[1,2,]) etc..., mean[200,3,].

Reid Huntsinger <reid_huntsinger at merck.com> replied:
> In your example you could use rowMeans(ar) as well, I think.

Specifically: rowMeans(ar, dims=2)
This gives the same result as apply(ar, 1:2, mean), but is faster.
Note rowMeans() and friends were introduced in R-1.5.0.
-- 
                              -- David Brahm (brahm at alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list