【问题标题】:Nesting tibbles after grouping by factor variables produces NULL elements in R按因子变量分组后嵌套小标题会在 R 中产生 NULL 元素
【发布时间】:2019-02-28 15:04:20
【问题描述】:

当我偶然发现几个错误时,我正在查看我的一些旧 R 代码。

在运行每一行并处理我的数据后,我发现 tidyr::nest()ing tibbles dplyr::group(ed)_by 因子变量产生了一个或多个 NULL 元素。

这里是 mtcars 数据的示例:

library(dplyr)
library(tidyr)

mtcars %>% 
  as_tibble() %>% 
  select(cyl, carb, mpg) %>% 
  mutate(cyl = factor(cyl),
         carb = factor(carb)) %>% 
  group_by(cyl, carb) %>% 
  nest()

# A tibble: 9 x 3
# cyl   carb  data            
# <fct> <fct> <list>          
#   1 6     4     <NULL>          
#   2 4     1     <tibble [5 x 1]>
#   3 6     1     <tibble [3 x 1]>
#   4 8     2     <NULL>          
#   5 8     4     <NULL>          
#   6 4     2     <tibble [6 x 1]>
#   7 8     3     <NULL>          
#   8 6     6     <NULL>          
#   9 8     8     <NULL> 

我认为nest() 正在考虑as.numeric() 的因素,并且当不同的变量呈现同名组时会“感到困惑”。但后来我尝试了:

mtcars %>% 
  as_tibble() %>% 
  select(cyl, carb, mpg) %>%  
  mutate(cyl = factor(cyl) %>% as.numeric(),
         carb = factor(carb) %>% as.numeric()) %>% 
  group_by(cyl, carb) %>% 
  nest()

并得到与嵌套非因子变量时相同的结果:

# A tibble: 9 x 3
# cyl  carb data            
# <dbl> <dbl> <list>          
#   1     2     4 <tibble [4 x 1]>
#   2     1     1 <tibble [5 x 1]>
#   3     2     1 <tibble [2 x 1]>
#   4     3     2 <tibble [4 x 1]>
#   5     3     4 <tibble [6 x 1]>
#   6     1     2 <tibble [6 x 1]>
#   7     3     3 <tibble [3 x 1]>
#   8     2     5 <tibble [1 x 1]>
#   9     3     6 <tibble [1 x 1]>

比较:

mtcars %>% 
  as_tibble() %>% 
  select(cyl, carb, mpg) %>% 
  group_by(cyl, carb) %>% 
  nest()

# A tibble: 9 x 3
# cyl  carb data            
# <dbl> <dbl> <list>          
#   1     6     4 <tibble [4 x 1]>
#   2     4     1 <tibble [5 x 1]>
#   3     6     1 <tibble [2 x 1]>
#   4     8     2 <tibble [4 x 1]>
#   5     8     4 <tibble [6 x 1]>
#   6     4     2 <tibble [6 x 1]>
#   7     8     3 <tibble [3 x 1]>
#   8     6     6 <tibble [1 x 1]>
#   9     8     8 <tibble [1 x 1]>

由于我的代码在上个月之前一直运行良好,我想知道 tidyr 最近是否更新了 nest() 处理因子组的方式是否改变了?

一般建议不要嵌套按因子变量分组的数据,还是不要嵌套在因子变量上的group_by()

编辑: 在 aosmith 提到的 issue 中,Hadley 引用了 group_nest(),这似乎解决了问题(警告:此函数重新排序 tibble!)。尽管如此,我仍然想知道为什么 nest() 会产生 NULL...

mtcars %>% 
  as_tibble() %>% 
  select(cyl, carb, mpg) %>% 
  mutate(cyl = factor(cyl),
         carb = factor(carb)) %>% 
  group_by(cyl, carb) %>%
  group_nest() %>% 
  unnest %>% 
  all.equal(.,
            mtcars %>% 
              as_tibble() %>% 
              select(cyl, carb, mpg) %>% 
              mutate(cyl = factor(cyl),
                     carb = factor(carb)))

# [1] TRUE

【问题讨论】:

  • 可能与this issue有关,与dplyr最新版本的变化有关。基于该讨论,我相信这已在 tidyr 的开发版本中得到修复。

标签: r group-by dplyr nested tidyr


【解决方案1】:

正如 aosmith 所建议的,这个问题最近已在 tidyr 的开发版中得到修复。由于我没有从链接的问题中认识到这一点并且我无法安装开发版本,因此我将此问题作为另一个issue 提交。哈德利刚刚回答。

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 2021-12-05
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多