[R] mean of 3D arrays

David Winsemius dwinsemius at comcast.net
Wed Oct 5 15:32:16 CEST 2011


On Oct 5, 2011, at 8:14 AM, R. Michael Weylandt <michael.weylandt at gmail.com 
 > wrote:

> (x1+x2+x3)/3
>
> I'm not aware of a "pmean" function but it wouldn't be hard to  
> homebrew one if you are comfortable with the ... argument
>
> I'll draft one up and send it along

pmean <- function(lis) Reduce("+",lis)/length(lis)

res <- pmean( list(x1,x2,x3) )

 > str(res)
  num [1:10, 1:10, 1:10] -0.879 0.843 -2.184 1.33 -0.675 ...

-- 
David.

>
> Michael Weylandt
>
> On Oct 5, 2011, at 9:00 AM, Martin Batholdy  
> <batholdy at googlemail.com> wrote:
>
>> Hi,
>>
>> I have multiple three dimensional arrays.
>>
>> Like this:
>>
>> x1 <- array(rnorm(1000, 1, 2), dim=c(10, 10, 10))
>> x2 <- array(rnorm(1000, 1, 2), dim=c(10, 10, 10))
>> x3 <- array(rnorm(1000, 1, 2), dim=c(10, 10, 10))
>>
>>
>> Now I would like to compute the mean for each corresponding cell.
>> As a result I want to get one 3D array (10 x 10 x 10) in which at  
>> position x, y, z is the mean of the corresponding values of x1, x2  
>> and x3 (at position x, y, z).
>>
>>
>> How can I do this?
>> e.



More information about the R-help mailing list