轻按和均值提供不同的答案

时间:2019-09-02 13:03:15

标签: r mean tapply

我正在处理玩具数据集。我注意到,通过子集数据并在完整数据集上使用tapply()来询问mean()会提供不同的答案。

dat<-data.frame(x1=c(1.34,1.19,0.89,0.88,0.98,0.85,0.93,0.87,1.02,0.96,
                     0.77,1.17,1.12,1.61,1.18,1.00,0.88,1.08,1.12,0.69,
                     1.29,1.16,0.94,0.94,0.58,0.87,1.11,1.08,0.92,1.23,
                     0.94,0.41,0.57,0.19,1.13,0.33,0.60,0.16,0.33,0.77,
                     1.44,0.62,0.70,0.77,0.47,0.44,0.28,0.22),
                 x2=c("+/+","+/+","+/+","+/+","+/+","+/+","+/+","+/+",
                      "+/+","+/+","+/+","+/+","+/+","+/+","+/-","+/-",
                      "+/-","+/-","+/-","+/-","+/-","+/-","+/-","+/-",
                      "+/-","+/-","+/-","+/-","+/-","+/-","+/-","+/-",
                      "-/-","-/-","-/-","-/-","-/-","-/-","-/-","-/-",
                      "-/-","-/-","-/-","-/-","-/-","-/-","-/-","-/-")
             )
 mean(dat$x1[which(dat$x2=="-/-")])
[1] 0.56
 mean(dat$x1[which(dat$x2=="+/-")])
[1] 0.97
 mean(dat$x1[which(dat$x2=="+/+")])
[1] 1
 tapply(X=dat$x1,INDEX=dat$x2,FUN=mean)
 -/-  +/-  +/+ 
0.56 0.97 1.04 
 mean(c(1.34,1.19,0.89,0.88,0.98,0.85,0.93,0.87,1.02,0.96,
       0.77,1.17,1.12,1.61))
[1] 1

tapply应该为1时如何返回1.04?

0 个答案:

没有答案