基于列值的行总数 [英] Sum of rows based on column value
本文介绍了基于列值的行总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对一列中具有相同值的行求和:
I want to sum rows that have the same value in one column:
> df <- data.frame("1"=c("a","b","a","c","c"), "2"=c(1,5,3,6,2), "3"=c(3,3,4,5,2))
> df
X1 X2 X3
1 a 1 3
2 b 5 3
3 a 3 4
4 c 6 5
5 c 2 2
对于一列(X2),可以汇总数据以获取具有相同X1值的所有行的总和:
For one column (X2), the data can be aggregated to get the sums of all rows that have the same X1 value:
> ddply(df, .(X1), summarise, X2=sum(X2))
X1 X2
1 a 4
2 b 5
3 c 8
我如何对X3和除X1以外的任意数量的其他列执行相同的操作?
这是我想要的结果:
X1 X2 X3
1 a 4 7
2 b 5 3
3 c 8 7
推荐答案
ddply(df, "X1", numcolwise(sum))
有关详细信息和示例,请参见?numcolwise
.
see ?numcolwise
for details and examples.
这篇关于基于列值的行总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文