【问题标题】:dplyr group_by summarise inconsistent number of rowsdplyr group_by 汇总不一致的行数
【发布时间】:2018-02-14 00:40:15
【问题描述】:

我一直在关注DataCamp 上的教程。我有以下代码行,当我运行它时,它会为“drows”产生不同的值

hflights %>% 
group_by(UniqueCarrier, Dest) %>% 
summarise(rows= n(), drows = n_distinct(rows))

第一次:

Source: local data frame [234 x 4]
Groups: UniqueCarrier [?]

        UniqueCarrier  Dest  rows drows
                <chr> <chr> <int> <int>
1             AirTran   ATL   211    86
2             AirTran   BKG    14     6
3              Alaska   SEA    32    18
4            American   DFW   186    74
5            American   MIA   129    57
6      American_Eagle   DFW   234   101
7      American_Eagle   LAX    74    34
8      American_Eagle   ORD   133    56
9  Atlantic_Southeast   ATL    64    28
10 Atlantic_Southeast   CVG     1     1
# ... with 224 more rows

第二次:

   Source: local data frame [234 x 4]
Groups: UniqueCarrier [?]

        UniqueCarrier  Dest  rows drows
                <chr> <chr> <int> <int>
1             AirTran   ATL   211   125
2             AirTran   BKG    14    13
3              Alaska   SEA    32    29
4            American   DFW   186   118
5            American   MIA   129    76
6      American_Eagle   DFW   234   143
7      American_Eagle   LAX    74    47
8      American_Eagle   ORD   133    85
9  Atlantic_Southeast   ATL    64    44
10 Atlantic_Southeast   CVG     1     1
# ... with 224 more rows

第三次:

Source: local data frame [234 x 4]
Groups: UniqueCarrier [?]

        UniqueCarrier  Dest  rows drows
                <chr> <chr> <int> <int>
1             AirTran   ATL   211    88
2             AirTran   BKG    14     7
3              Alaska   SEA    32    16
4            American   DFW   186    79
5            American   MIA   129    61
6      American_Eagle   DFW   234    95
7      American_Eagle   LAX    74    31
8      American_Eagle   ORD   133    67
9  Atlantic_Southeast   ATL    64    31
10 Atlantic_Southeast   CVG     1     1
# ... with 224 more rows

我的问题是为什么这个值会不断变化?它在做什么?

【问题讨论】:

  • drows = n_distinct(rows) 在这里应该一直等于 1。我不明白怎么回事
  • 我们有相同的登录名也很有趣
  • 可能是他们的在线 shell 的错误?有趣的是,我们有相同的登录名,对同一个问题发表评论的几率有多大。
  • hflights 是 CRAN 上的一个包。我刚刚下载了软件包并测试了您的代码。它显示了同样的问题。
  • 对我来说也一样。然后它让我的 rstudio 崩溃了。

标签: r dplyr


【解决方案1】:

显然这是正常行为,请在此处查看此问题。 https://github.com/tidyverse/dplyr/issues/2222

这是因为列表列中的值是通过引用进行比较的,所以 n_distinct() 将它们视为不同的,除非它们确实指向 同一个对象:

所以 df 的内部存储改变了事情的工作方式。 Hadley 在那个问题上的评论似乎说这可能是一个错误(在不受欢迎的行为的意义上),或者这可能是他们需要更好地记录的预期行为。

【讨论】:

  • 好的,这就解释了为什么结果不是 1。我仍然不明白为什么每次运行命令时值都会变化。
  • 身份证。我对此太深入了,发现了一些我不明白的东西。
  • 如果我直接调用它(就像你做的那样),那就搞砸了。如果我将它包装在一个函数中(如f &lt;- function(x) { n_distinct(x) },然后在summarise 中调用它,那很好。如果我调用drows = n_distinct(c(rows)),它可以工作。我不明白为什么,除了改变你对@ 的寻址方式987654325@ 改变了数据的内部表示,这使得一种方式有效,另一种方式失败。
  • 同时执行mtcars %&gt;% summarise(n = n(), drows = n_distinct(n())) 会给出正确答案 (drows = 1)。而使用mutate 也可以,mtcars %&gt;% summarise(n = n(), drows = n_distinct(n))。但是summarisen_distinct 以及在summarise 中重新使用创建的变量,这搞砸了。 (如果你使用n()以外的函数也会失败。)?我放弃了。
猜你喜欢
  • 1970-01-01
  • 2016-09-25
  • 2022-01-10
  • 2022-07-21
  • 2016-09-17
  • 2021-01-19
  • 1970-01-01
  • 2015-04-18
  • 2021-12-15
相关资源
最近更新 更多