Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 多维平均值';阵列内观测_Arrays_R_Mean - Fatal编程技术网

Arrays 多维平均值';阵列内观测

Arrays 多维平均值';阵列内观测,arrays,r,mean,Arrays,R,Mean,我得到一个数组,如下所示: ar.1 = array(1:12, dim=c(2,2,3)) > ar.1 , , 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 我只需要计算3个不同层中的值之间的平均值,然后得到一个二维矩阵 预

我得到一个数组,如下所示:

ar.1 = array(1:12, dim=c(2,2,3))

> ar.1
, , 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
我只需要计算3个不同层中的值之间的平均值,然后得到一个二维矩阵

预期产出:

5  7  
6  8


5 = (1+5+9) / 3
6 = (2+6+10) / 3
etc..
这应该很容易,但我还找不到任何快速解决方案,因为我的实际阵列大于2x2x3

谢谢你试试这个

matrix(rowMeans(apply(ar.1, 3, c)), dim(ar.1)[1], dim(ar.1)[2])

#      [,1] [,2]
#[1,]    5    7
#[2,]    6    8