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?
mean of 3D arrays
5 messages · Martin Batholdy, David Winsemius, michael.weylandt at gmail.com (R. Michael Weylandt +1 more
(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 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?
______________________________________________ R-help at 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.
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.
As promised
### Untested
pmean <- function(...){
dotArgs <- list(...)
l <- length(dotArgs)
if( l == 0L ) stop("no arguments")
temp <- dotArgs[[1]]
if ( l > 1L ) {for(i in 2L:l) {temp <- temp + dotArgs[[i]]}}
temp/l
}
Clunky but gets the job done. Its still too early for me to think straight so I'll let someone else kill the loop and add error checking if interested.
Michael Weylandt
On Oct 5, 2011, at 9:14 AM, "R. Michael Weylandt <michael.weylandt at gmail.com>" <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 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?
______________________________________________ R-help at 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.
Hi:
There are a few ways to do this. If you only have a few arrays, you
can simply add them and divide by the number of arrays. If you have a
large number of such arrays, this is inconvenient, so an alternative
is to ship the arrays into a list and use the Reduce() function. For
your example,
L <- list(x1, x2, x3)
Reduce('+', L)/length(L)
would work. If you have many such arrays in separate files, you can
always use lapply() in conjunction with a suitable read function with
an input vector that contains the file names to be read, of the
general form
L <- lapply(<vector of file names>, <function to read data>)
with the idea that the read function passes arrays into the list components.
Here's a simple toy example with three small matrices to illustrate
proof of concept:
t1 <- matrix(1:9, nrow = 3) t2 <- matrix(-4:4, nrow = 3) t3 <- matrix(-3:5, nrow = 3) (t1 + t2 + t3)/3
[,1] [,2] [,3] [1,] -2 1 4 [2,] -1 2 5 [3,] 0 3 6
Reduce('+', list(t1, t2, t3))/3
[,1] [,2] [,3] [1,] -2 1 4 [2,] -1 2 5 [3,] 0 3 6 HTH, Dennis
On Wed, Oct 5, 2011 at 6: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?
______________________________________________ R-help at 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.