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

2013-08-29 Thread Jonathan Greenberg
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: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

2013-08-29 Thread arun
Hi,
You could try:
res-colMeans(aperm(moo,c(2,1,3)))
resOld-apply(moo,c(1,3),mean)
 identical(res,resOld)
#[1] TRUE

#Speed:
set.seed(285)
moo1- array(runif(1400*9*15),dim=c(1400,9,15))

system.time({res1- colMeans(aperm(moo1,c(2,1,3)))})
 #user  system elapsed 
 # 0.004   0.000   0.002 

system.time({res2- apply(moo1,c(1,3),mean)})
 # user  system elapsed 
 # 0.180   0.000   0.178 
identical(res1,res2)
#[1] TRUE


A.K.



- Original Message -
From: Jonathan Greenberg j...@illinois.edu
To: r-help r-help@r-project.org
Cc: 
Sent: Thursday, August 29, 2013 6:36 PM
Subject: [R] Vectorized version of colMeans/rowMeans for higher dimension   
arrays?

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: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.