数据框从现有行中添加新行

时间:2019-02-09 12:33:03

标签: r dplyr

我当前的数据:

Type             Country        Score
University       Australia       10
University       Brazil          10
University       Hong Kong       10
College          Australia       10
College          Brazil          10
College          Hong Kong       10

现在,我想总结一下根据上述数据创建的新行,如下所示。新的行是一个国家/地区中每所大学和学院的分数总和。

Type             Country         Score
University       Australia       10
University       Brazil          10
University       Hong Kong       10
College          Australia       10
College          Brazil          10
College          Hong Kong       10
All              Australia       20
All              Brazil          20
All              Honk Kong       20

我知道我可以编写循环以遍历数据并检查国家/地区,但是也许我可以使用dplyr之类的软件包来实现自己想要的。

3 个答案:

答案 0 :(得分:3)

我们需要group_by的“国家/地区”获得“得分”的sum,使用“全部”创建一个新的列“类型”,并将行与原始数据绑定

library(dplyr)
df1  %>% 
  group_by(Country) %>%
  summarise(Score = sum(Score)) %>%
  mutate(Type = "All") %>% 
  bind_rows(df1, .)
#        Type   Country Score
#1 University Australia    10
#2 University    Brazil    10
#3 University Hong Kong    10
#4    College Australia    10
#5    College    Brazil    10
#6    College Hong Kong    10
#7        All Australia    20
#8        All    Brazil    20
#9        All Hong Kong    20

答案 1 :(得分:3)

或者在基数R中,您可以使用aggregate

Extra = cbind("All", aggregate(df$Score, list(df$Country), sum))
names(Extra) = names(df)
rbind(df, Extra)
        Type   Country Score
1 University Australia    10
2 University    Brazil    10
3 University Hong Kong    10
4    College Australia    10
5    College    Brazil    10
6    College Hong Kong    10
7        All Australia    20
8        All    Brazil    20
9        All Hong Kong    20

答案 2 :(得分:2)

另一种使用ave()的方法:

df2 <- within(df1, {Type <- "All" ; Score <- ave(Score, Country, FUN = sum)})
rbind(df1, unique(df2))

#         Type   Country Score
# 1 University Australia    10
# 2 University    Brazil    10
# 3 University Hong Kong    10
# 4    College Australia    10
# 5    College    Brazil    10
# 6    College Hong Kong    10
# 7        All Australia    20
# 8        All    Brazil    20
# 9        All Hong Kong    20