为什么“总结”会掉队?

问题描述:

我鬼混的是 babynames pkg。 group_by 命令有效,但是在 summary 之后,其中一个组将从组列表中删除。

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

这很好-两个小组,年份,名称。但是,在进行总结(正确尊重组)之后,将删除 name 组。我想念一个容易犯的错误吗?

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


文档中已记录了该行为,请参见?总结 部分:

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



.data 相同类的对象。






相反, mutate 不会不会删除任何分组级别:


In contrast, mutate does not drop any grouping levels:


:与 .data 属于同一类的对象。

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