R 我可以为acast中的不同列使用不同的聚合规则吗?
今天的大脑功能:我如何告诉acast返回不同的聚合R 我可以为acast中的不同列使用不同的聚合规则吗?,r,aggregate,reshape2,R,Aggregate,Reshape2,今天的大脑功能:我如何告诉acast返回不同的聚合 # the rows and columns have integer names Rgames> foo 1 2 1 1 1 2 2 2 3 3 3 4 4 4 1 1 4 2 2 8 3 3 2 4 4 1 Rgames> mfoo<-melt(foo) Rgames> mfoo Var1 Var2 value 1 1 1 1 2 2 1 2 3 3
# the rows and columns have integer names
Rgames> foo
1 2
1 1 1
2 2 2
3 3 3
4 4 4
1 1 4
2 2 8
3 3 2
4 4 1
Rgames> mfoo<-melt(foo)
Rgames> mfoo
Var1 Var2 value
1 1 1 1
2 2 1 2
3 3 1 3
4 4 1 4
5 1 1 1
6 2 1 2
7 3 1 3
8 4 1 4
9 1 2 1
10 2 2 2
11 3 2 3
12 4 2 4
13 1 2 4
14 2 2 8
15 3 2 2
16 4 2 1
Rgames> acast(mfoo,Var1~Var2,function(x)x[1]-x[2])
1 2
1 0 -3
2 0 -6
3 0 1
4 0 3
# what I would like is the casting formula to return
1 2
1 1 -3
2 2 -6
3 3 1
4 4 3
rownames已经有了,你能接受吗?@Codoremifa我可能可以,但我怀疑有一个正确的方法。同样,在一般情况下,我必须将行名称跟踪回第一列中的实际floatingpoint值。谢谢你的建议,你不能。您可以通过使用plyr聚合,然后使用dcast@hadley好的,谢谢。最后,我在第一列中使用了intersect,并对结果进行了子集设置。
aggregate(foo[,2],by=list((foo[,1])),function(x)x[1]-x[2])