Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.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
计算R中几个向量的平均值_R_Average - Fatal编程技术网

计算R中几个向量的平均值

计算R中几个向量的平均值,r,average,R,Average,我有四个向量: a<-c(1,2,3,4,5) b<-c(2,3,4,5,6) c<-c(3,5,6,7,2) d<-c(5,7,3,7,2) 谁能给我一个建议,我该怎么做? 谢谢。将它们放在一个矩阵中,然后使用rowmeals: a<-c(1,2,3,4,5) b<-c(2,3,4,5,6) c1<-c(3,5,6,7,2) d<-c(5,7,3,7,2) rowMeans(matrix(c(a,b,c1,d), ncol=4)) ## [1]

我有四个向量:

a<-c(1,2,3,4,5)
b<-c(2,3,4,5,6)
c<-c(3,5,6,7,2)
d<-c(5,7,3,7,2)
谁能给我一个建议,我该怎么做?
谢谢。

将它们放在一个矩阵中,然后使用
rowmeals

a<-c(1,2,3,4,5)
b<-c(2,3,4,5,6)
c1<-c(3,5,6,7,2)
d<-c(5,7,3,7,2)
rowMeans(matrix(c(a,b,c1,d), ncol=4))
## [1] 2.75 4.25 4.00 5.75 3.75

a带有
mapply的解决方案

mapply(function(...) mean(c(...)), a, b, c, d)
l <- list(a,b,c,d)
sapply(seq_along(a), function(i) mean(sapply(l, function(x) x[i])))
使用
减少的解决方案

l <- list(a,b,c,d)
Reduce(`+`, l) / length(l)

如果您运行这些代码行,第四行将给出一个错误。
l <- list(a,b,c,d)
sapply(seq_along(a), function(i) mean(sapply(l, function(x) x[i])))