gpt4 book ai didi

r - R中第三维的均值

转载 作者:行者123 更新时间:2023-12-04 01:44:56 25 4
gpt4 key购买 nike

R中是否有任何快速方法或内置函数来计算基于第三维的平均值?

例如我的数组是:

, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4

, , 2
[,1] [,2]
[1,] 11 13
[2,] 12 14

, , 3
[,1] [,2]
[1,] 21 23
[2,] 22 24

我的输出是:
            [,1]        [,2]
[1,] mean(1,11,21) mean(3,13,23)
[2,] mean(2,12,22) mean(4,14,24)

谢谢!

最佳答案

?apply是您完成这些类型任务的 friend 。

# Make the sample data
j <- array(c(1:4, 11:14, 21:24), c(2,2,3))
# For each combination in the 1st and 2nd dimension
# average over the values in the 3rd.
apply(j, c(1,2), mean)

关于r - R中第三维的均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13058978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com