For the following array
z <- array(1:12, dim = c(2,2,3))
z
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
, , 3
[,1] [,2]
[1,] 9 11
[2,] 10 12
I would like to construct a 2x2
matrix by taking the mean value of the same position entry. My first entry is the average of 1,5,9
and the second entry is the average of 3,7,11
the third entry is the average of 2,6,10
and fourth entry is the average of 4,8,12
. Any help is appreciated.
We specify the MARGIN
as 1 and 2, and apply the mean
using apply
apply(z, c(1,2), mean)
# [,1] [,2]
#[1,] 5 7
#[2,] 6 8
Or specify one MARGIN and use rowMeans
apply(z, 1, rowMeans)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments