[R] Vectorized version of colMeans/rowMeans for higher dimension arrays?

Jonathan Greenberg jgrn at illinois.edu
Fri Aug 30 00:36:55 CEST 2013


For matrices, colMeans/rowMeans are quick, vectorized functions.  But
say I have a higher dimensional array:

moo <- array(runif(400*9*3),dim=c(400,9,3))

And I want to get the mean along the 2nd dimension.  I can, of course,
use apply:

moo1 <- apply(moo,c(1,3),mean)

But this is not a vectorized operation (so it doesn't execute as
quickly).  How would one vectorize this operation (if possible)?  Is
there an array equivalent of colMeans/rowMeans?

--j

-- 
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
607 South Mathews Avenue, MC 150
Urbana, IL 61801
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007



More information about the R-help mailing list