在R中没有正确计算平均值?

时间:2014-09-04 15:06:51

标签: r average

我很难理解为什么会这样:

a<-c(1.1,1.2,1.3)
b<-c(2.1,2.3,2.6)
c<-c(1.6,2.3,2.6)

mean<-rowMeans(matrix(c(a,b,c),ncol=3))  
mean  #### This are the mean values
[1] 1.600000 1.933333 2.166667

mean(a[1],b[1],c[1])  #### trying to calculate the mean of 1.1 ,2.1 and 1.6
[1] 1.1  #### Why is this not 1.6??

3 个答案:

答案 0 :(得分:14)

R的争论匹配让你感到高兴:

> mean(9999,2,3,4,5)
[1] 9999

help(mean)说:

Usage:

     mean(x, ...)

然后说它计算x的平均值并将...传递给其他方法。在我的示例中,9999之后的所有数字都以点 - 点形式捕获。

R然后调用mean.default,因为9999只是一个数字,mean.default对点 - 点参数没有任何作用,包括错误,如果有任何内容他们。

您可以使用它为函数调用添加任意无用的东西:

> mean(c(1,2,3,4), monkeys=TRUE)
[1] 2.5

答案 1 :(得分:5)

您对meanmean(a[1],b[1],c[1])的来电仅对第一个元素进行操作,即mean(a[1])。你需要像这样连接a[1],b[1],c[1]

> mean(c(a[1],b[1],c[1]))
[1] 1.6

答案 2 :(得分:0)

Mabye你的问题是你的矩阵被转置? 试试这个:

mean<-rowMeans(matrix(c(a,b,c),ncol=3, byrow=T)) 
相关问题