根据R

时间:2016-01-13 15:37:47

标签: r

我有以下数据:

 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

非常感谢任何帮助!

2 个答案:

答案 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)]