我有以下数据:
State Name Population
1 NY New York 1
2 NJ New Jersey 2
3 CA California 1
4 RI Rhode Island 1
5 NY New York 1
我想使用R来总结state列和state列的所有唯一组合。所以最终结果将是:
State Name Population
1 NJ New Jersey 2
2 NY New York 2
3 CA California 1
4 RI Rhode Island 1
非常感谢任何帮助!
答案 0 :(得分:0)
您可以使用dplyr包执行以下操作:
library(dplyr)
df %>% group_by(State, Name) %>% summarise(Population = sum(Population))
答案 1 :(得分:0)
我们可以使用aggregate
base R
aggregate(Population~., df1, sum)
或data.table
library(data.table)
setDT(df1)[, list(Population = sum(Population)), .(State, Name)]