【发布时间】:2014-09-03 09:41:00
【问题描述】:
我有以下数据框:
pt_no = rep(1:10, each=18)
group = rep(c('gp1','gp2'), each=90)
test = rep(1:6, each=3, length=180)
month = rep(c(0,1,3), length=180)
value = runif(180, 100,200)
oridf = data.frame(pt_no, group, test, month, value)
head(oridf)
pt_no group test month value
1 1 gp1 1 0 114.7907
2 1 gp1 1 1 119.3668
3 1 gp1 1 3 135.8100
4 1 gp1 2 0 124.4290
5 1 gp1 2 1 156.0008
6 1 gp1 2 3 115.7246
>
我必须根据 'test'、'group' 和 'month' 找到方法来制作如下表:
test_no gp1_0month gp2_0month gp1_1month gp2_1month gp1_3month gp2_3month
Test_1 136 137 152 143 156 150
Test_2 130 129 81 78 86 80
Test_3 129 128 68 68 74 71
Test_4 40 40 45 43 47 46
Test_5 203 201 141 134 149 142
Test_6 170 166 134 116 139 125
(上表平均值仅供参考)
我可以使用 tapply,但它给了我 2 张桌子:
tapply(oridf$value, list(test,month,group), mean)
, , gp1
0 1 3
1 147.5239 145.7311 151.6526
2 157.8421 131.0775 144.3387
3 144.2670 146.8478 170.7292
4 150.6332 172.0349 147.2165
5 131.4145 161.2294 143.2634
6 142.6708 150.4848 160.5059
, , gp2
0 1 3
1 142.3145 157.7935 152.4228
2 131.5410 163.1386 145.8485
3 134.6620 136.7388 167.1557
4 122.4177 164.5213 124.0728
5 154.2681 165.0370 152.8372
6 154.4926 141.0391 147.2471
如何获得一个平均值表?感谢您的帮助。
【问题讨论】: