我在R中有一个看起来像这样的表:
"Dimension","Config","Result"
"3","1","6.43547800901942e-12"
"3","1","3.10671396584125e-15"
"3","1","5.86997050075184e-07"
"3","2","1.57865350726808"
"3","2","0.125293574811717"
"3","2","0.096173751923243"
"4","1","3.33845065295529e-08"
"4","1","4.57511389653726e-07"
"4","1","2.58918409465438e-07"
"4","2","3.23375251723051"
"4","2","2.13142950121767"
"4","2","0.510008166587752"
可以看出,我每个维度总是有6个值,而对于每个维度,我分别有3个值的配置1和3个值的配置2。是否可以“双重汇总”此表,以便输出每个维度的配置1的平均值以及每个维度的config 2的平均值?
如果我使用以下命令行:
a <- aggregate(d[,3], list(d$Dimension), mean)
我得到这个结果:
Group.1 x
1 3 3.000202e-01
2 4 9.791985e-01
但是我想要这样的东西:
Group.1 Config x
1 3 1 <mean value for this row>
2 3 2 <mean value for this row>
3 4 1 <mean value for this row>
4 4 2 <mean value for this row>
答案 0 :(得分:1)
您可以使用公式界面。
d <- read.table(text="Dimension,Config,Result
3,1,6.43547800901942e-12
3,1,3.10671396584125e-15
3,1,5.86997050075184e-07
3,2,1.57865350726808
3,2,0.125293574811717
3,2,0.096173751923243
4,1,3.33845065295529e-08
4,1,4.57511389653726e-07
4,1,2.58918409465438e-07
4,2,3.23375251723051
4,2,2.13142950121767
4,2,0.510008166587752", header=T, sep=',')
aggregate(Result ~ Dimension+Config, data=d, mean)
Dimension Config Result
1 3 1 1.956678e-07
2 4 1 2.499381e-07
3 3 2 6.000403e-01
4 4 2 1.958397e+00