Why does `summarize` drop a group?

假如想象 提交于 2020-01-11 06:13:10

问题


I'm fooling around with babynames pkg. A group_by command works, but after the summarize, one of the groups is dropped from the group list.

library(babynames)
babynames[1:10000, ] %>% group_by(year, name) %>% head(1)

# A tibble: 1 x 5
# Groups:   year, name [1]
   year   sex  name     n       prop
  <dbl> <chr> <chr> <int>      <dbl>
1  1880     F  Mary  7065 0.07238433

This is fine---two groups, year, name. But after a summarize (which respects the groups correctly), the name group is dropped. Am I missing an easy mistake?

babynames[1:10000, ] %>% 
    group_by(year, name) %>% 
    summarise(n = sum(n)) %>% head(1)

# A tibble: 1 x 3
# Groups:   year [1]
   year  name     n
  <dbl> <chr> <int>
1  1880 Aaron   102

More info, in case it's relevant:

R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.13.2
dplyr_0.7.4

回答1:


The behavior is noted in the documentation see ?summarise Value section:

Value An object of the same class as .data. One grouping level will be dropped.


In contrast, mutate does not drop any grouping levels:

Value: An object of the same class as .data.



来源:https://stackoverflow.com/questions/48489219/why-does-summarize-drop-a-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!