[R] apply mean to a three-dimension data
Patrick Hausmann
patrick.hausmann at uni-bremen.de
Fri Mar 25 07:31:59 CET 2011
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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list