为什么“总结”会掉队? [英] Why does `summarize` drop a group?

查看:94
本文介绍了为什么“总结”会掉队?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我鬼混的是 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.

这篇关于为什么“总结”会掉队?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆