使用group_by()和nest()产生奇怪的输出

时间:2019-08-22 15:24:56

标签: dplyr tidyr

当我想使用group_by()并接着使用nest()命令创建列表列时(如nest()的帮助文件中所示),我得到了意外的结果。当我在未分组的数据帧中使用nest()时,输出是预期的。我还没有找到发生这种情况的原因,但是在我更新到最新的tidyr版本之后就开始了。有解决方案吗?

library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang

fish_encounters %>% 
  group_by(fish) %>% 
  nest()
#>    fish                                                               data
#> 1  4842 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
#> 2  4843 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
#> 3  4844 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
#> 4  4845                                       1, 2, 3, 4, 5, 1, 1, 1, 1, 1
#> 5  4847                                                   1, 2, 3, 1, 1, 1
#> 6  4848                                             1, 2, 3, 4, 1, 1, 1, 1
#> 7  4849                                                         1, 2, 1, 1
#> 8  4850                                 1, 2, 4, 5, 6, 7, 1, 1, 1, 1, 1, 1
#> 9  4851                                                         1, 2, 1, 1
#> 10 4854                                                         1, 2, 1, 1
#> 11 4855                                       1, 2, 3, 4, 5, 1, 1, 1, 1, 1
#> 12 4857               1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1
#> 13 4858 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
#> 14 4859                                       1, 2, 3, 4, 5, 1, 1, 1, 1, 1
#> 15 4861 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
#> 16 4862               1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1
#> 17 4863                                                         1, 2, 1, 1
#> 18 4864                                                         1, 2, 1, 1
#> 19 4865                                                   1, 2, 3, 1, 1, 1

fish_encounters %>% 
  nest(data = c(station, seen))
#> # A tibble: 19 x 2
#>    fish            data
#>    <fct> <list<df[,2]>>
#>  1 4842        [11 × 2]
#>  2 4843        [11 × 2]
#>  3 4844        [11 × 2]
#>  4 4845         [5 × 2]
#>  5 4847         [3 × 2]
#>  6 4848         [4 × 2]
#>  7 4849         [2 × 2]
#>  8 4850         [6 × 2]
#>  9 4851         [2 × 2]
#> 10 4854         [2 × 2]
#> 11 4855         [5 × 2]
#> 12 4857         [9 × 2]
#> 13 4858        [11 × 2]
#> 14 4859         [5 × 2]
#> 15 4861        [11 × 2]
#> 16 4862         [9 × 2]
#> 17 4863         [2 × 2]
#> 18 4864         [2 × 2]
#> 19 4865         [3 × 2]

sessionInfo()
#> R version 3.6.0 (2019-04-26)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS Sierra 10.12.6
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] de_CH.UTF-8/de_CH.UTF-8/de_CH.UTF-8/C/de_CH.UTF-8/de_CH.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] forcats_0.4.0    stringr_1.4.0    dplyr_0.8.3      purrr_0.3.2     
#> [5] readr_1.3.1      tidyr_0.8.3.9000 tibble_2.1.3     ggplot2_3.1.1   
#> [9] tidyverse_1.2.1 
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.1       cellranger_1.1.0 plyr_1.8.4       pillar_1.4.2    
#>  [5] compiler_3.6.0   highr_0.8        tools_3.6.0      zeallot_0.1.0   
#>  [9] digest_0.6.20    lubridate_1.7.4  jsonlite_1.6     evaluate_0.14   
#> [13] nlme_3.1-140     gtable_0.3.0     lattice_0.20-38  pkgconfig_2.0.2 
#> [17] rlang_0.4.0.9000 cli_1.1.0        yaml_2.2.0       haven_2.1.0     
#> [21] xfun_0.7         withr_2.1.2      xml2_1.2.0       httr_1.4.0      
#> [25] knitr_1.23       hms_0.4.2        generics_0.0.2   vctrs_0.2.0     
#> [29] grid_3.6.0       tidyselect_0.2.5 glue_1.3.1       R6_2.4.0        
#> [33] fansi_0.4.0      readxl_1.3.1     rmarkdown_1.13   modelr_0.1.4    
#> [37] magrittr_1.5     backports_1.1.4  scales_1.0.0     htmltools_0.3.6 
#> [41] rvest_0.3.4      assertthat_0.2.1 colorspace_1.4-1 utf8_1.1.4      
#> [45] stringi_1.4.3    lazyeval_0.2.2   munsell_0.5.0    broom_0.5.2     
#> [49] crayon_1.3.4

reprex package(v0.3.0)于2019-08-22创建

1 个答案:

答案 0 :(得分:0)

似乎这与tidyr的dev版本内部有关,其中nest()返回data.frame而不是小标题。参见https://github.com/tidyverse/tidyr/issues/675

这样做可以克服

fish_encounters %>% 
  group_by(fish) %>% 
  nest() %>%
  as_tibble()

您可以使用以下方法重新安装vctrs以解决该问题:

remotes::install_github("r-lib/vctrs")

然后重新启动R。