# Re: [R] apply mean to a three-dimension data

From: Patrick Hausmann <patrick.hausmann_at_uni-bremen.de>
Date: Fri, 25 Mar 2011 07:31:59 +0100

Hi,
I think you could also use this way (via array, see http://r.789695.n4.nabble.com/apply-over-list-of-data-frames-td3057968.html)

b <- list()

```b[[1]] = matrix(1:4, 2, 2)
b[[2]] = matrix(10:13, 2, 2)
b[[3]] = matrix(20:23, 2, 2)

```

b.a <- array(unlist(b), dim=c(2, 2, 3))

(b.mean <- apply(X = b.a, MARGIN = c(1, 2), FUN = mean))

(b.sum <- apply(X = b.a, MARGIN = c(1, 2), FUN = sum))

HTH
Patrick

Am 24.03.2011 16:07, schrieb Hui Du:
> Hi All,
>
> Suppose I have data like
>
> b[[1]] = matrix(1:4, 2, 2)
> b[[2]] = matrix(10:13, 2, 2)
> b[[3]] = matrix(20:23, 2, 2)
>
> [[1]]
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
>
> [[2]]
> [,1] [,2]
> [1,] 10 12
> [2,] 11 13
>
> [[3]]
> [,1] [,2]
> [1,] 20 22
> [2,] 21 23
>
> Now I want to calculate the mean of each cell across the list. For example mean of (b[[1]][1,1], b[[2]][1,1], b[[3]][1,1]), mean of (b[[1]][1,2], b[[2]][1,2], b[[3]][1,2]) etc. e.g. mean of (1, 10, 20), mean of(3, 12, 22). Could somebody tell me how to do it? Thank you in advance.
>
> HXD
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help