此问题已经在这里有了答案:
我有一个数据框,例如:
id country color amount
1 US green 2
1 US green 1
2 Spain yellow 2
3 Portugal red 4
3 Portugal red 2
我想通过拥有唯一的ID来加入共享ID的所有行,并添加数量。结果将是:
id country color amount
1 US green 3
2 Spain yellow 2
3 Portugal red 6
tapply(amount, id, sum)
或合计:
aggrgate(amount, list(id), sum)