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
Psidom
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