【问题标题】:Getting "Can't subset columns that don't exist." when the column does exist with dplyr得到“不能对不存在的列进行子集化。”当 dplyr 确实存在该列时
【发布时间】:2020-12-25 09:57:20
【问题描述】:

我已将变量“县”列为一列,但当我尝试以这种方式使用 group_by_across 聚合它时:

testing4 <- testing2 %>%
        group_by(across(-c(county, population))) %>%
        summarise(pop=sum(population))

它给了我:

Error: Problem with `mutate()` input `..1`.
x Can't subset columns that don't exist.
x Column `county` doesn't exist.
Input `..1` is `across(-c(county, population))`.
i The error occurred in group 1: year = 1980, state = "AK", stfips = 2, 
county = 2900.
Run `rlang::last_error()` to see where the error occurred.

但是,当我这样做时

testing3 <- testing2 %>%
        group_by(year, state, stfips, race) %>%
        summarise(pop = sum(population))

它运行良好。

编辑:有人要求 dput(head(testing2))

dput(head(testing2))
structure(list(year = c(1980L, 1980L, 1980L, 1980L, 1980L, 1980L
), state = c("AK", "AK", "AK", "AL", "AL", "AL"), stfips = c(2L, 
2L, 2L, 1L, 1L, 1L), county = c(2900L, 2900L, 2900L, 1001L, 1001L, 
1001L), race = c(1L, 2L, 3L, 1L, 2L, 3L), population = c(318054L, 
13960L, 72666L, 24876L, 7193L, 148L)), row.names = c(NA, -6L), groups = 
structure(list(
year = c(1980L, 1980L), state = c("AK", "AL"), stfips = 2:1, 
county = c(2900L, 1001L), .rows = structure(list(1:3, 4:6), ptype = 
integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

【问题讨论】:

  • 似乎across() 可能会导致问题。您没有在工作版本中使用它。如果您提供带有样本输入和所需输出的reproducible example,会更容易提供帮助。随意为您的示例使用内置数据集,而不是您自己的数据。
  • 当我做iris %&gt;% group_by(across(-c(Sepal.Length, -Petal.Length))) 时,它似乎有效。所以也许它真的是你特定的data.frame。分享dput(),前几行就可以了。 dput(head(testing2))
  • 我看到您正在使用的数据集已经分组。在做更多工作之前,您是否尝试过取消分组?
  • 请参阅https://github.com/tidyverse/dplyr/issues/5253 - 如果数据已按其中一个变量分组,则使用 across()group_by() 会失败。虽然错误消息不是特别有用。

标签: r dplyr


【解决方案1】:

看起来像插入ungroup(),因为第二步会起作用:为了最小的比较,比较

testing2 %>% group_by(across(-county))

testing2 %>% ungroup() %>% group_by(across(-county))

【讨论】:

    【解决方案2】:

    欢迎来到这里。

    当你对 tibble 进行分组时,分组后应用的所有函数都使用分组数据,不包括分组变量 (?group_by)。事实上,您可以使用cur_group() (?cur_across) 在该函数中使用/访问该(每个组临时新的)数据。

    所以,第 1 步:当您在分组的 tibble(如您的)中使用 across() 时,对于每个组,它使用没有分组变量的数据。 across()(没有 .fnc 参数,默认 = NULL;?across)从输入数据开始,不加修改地返回列出的变量,在您的情况下,它没有旧的分组变量!因此,您不能在 across() 中使用分组变量作为分组的 tibble。

    但是,第 2 步:您也可以认为 group_by() 会覆盖自身(参见 ?group_by 中的示例)。

    将两者结合起来,如果它已经是一个分组变量,则无需列出要排除的变量。如果您要根据其他变量(重新)分组一个小标题:您可以删除您不想使用的其他小标题!在您计算新组时,这些变量(以及用于先前分组的其他变量)已经被排除在外。当新的group_by(评估across()“按组”;即没有分组变量)加入结果时,它返回分组的整个小标题,没有以前的分组变量,也没有你刚刚“添加”到排除。

    如果您想重新分组一个分组的小标题,排除其他变量但保留分组的(一个子集),可能会出现一个问题。无论如何,在这些情况下,您可以在对 across() 的调用之外列出那些“维护的”分组变量到对 group_by() 的调用中(它本身不会“计算”任何东西(与 across() 相反),所以它确实如此不要使用分组 tibble 的部分(没有旧的分组变量))。这样最后一个group_by() 创建了一个分组标题“所有不在旧分组变量中的变量,没有在新排除的变量中列出,加上在across() 之外报告的(旧)变量。”

    这是一个正在运行的 (reproducible) 示例:

    # install.packages("tidyverse")
    # install.packages("palmerpenguins")
    
    library(tidyverse)
    library(palmerpenguins)
    
    penguins
    #> # A tibble: 344 x 8
    #>    species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g
    #>    <fct>   <fct>           <dbl>         <dbl>            <int>       <int>
    #>  1 Adelie  Torge…           39.1          18.7              181        3750
    #>  2 Adelie  Torge…           39.5          17.4              186        3800
    #>  3 Adelie  Torge…           40.3          18                195        3250
    #>  4 Adelie  Torge…           NA            NA                 NA          NA
    #>  5 Adelie  Torge…           36.7          19.3              193        3450
    #>  6 Adelie  Torge…           39.3          20.6              190        3650
    #>  7 Adelie  Torge…           38.9          17.8              181        3625
    #>  8 Adelie  Torge…           39.2          19.6              195        4675
    #>  9 Adelie  Torge…           34.1          18.1              193        3475
    #> 10 Adelie  Torge…           42            20.2              190        4250
    #> # … with 334 more rows, and 2 more variables: sex <fct>, year <int>
    
    penguins %>% 
        group_by(species, island) %>% 
        group_by(across(-c(
            starts_with("bill"),
            starts_with("flipper"),
            starts_with("body")
        ))) # species and island are already exluded
    #> # A tibble: 344 x 8
    #> # Groups:   sex, year [9]
    #>    species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g
    #>    <fct>   <fct>           <dbl>         <dbl>            <int>       <int>
    #>  1 Adelie  Torge…           39.1          18.7              181        3750
    #>  2 Adelie  Torge…           39.5          17.4              186        3800
    #>  3 Adelie  Torge…           40.3          18                195        3250
    #>  4 Adelie  Torge…           NA            NA                 NA          NA
    #>  5 Adelie  Torge…           36.7          19.3              193        3450
    #>  6 Adelie  Torge…           39.3          20.6              190        3650
    #>  7 Adelie  Torge…           38.9          17.8              181        3625
    #>  8 Adelie  Torge…           39.2          19.6              195        4675
    #>  9 Adelie  Torge…           34.1          18.1              193        3475
    #> 10 Adelie  Torge…           42            20.2              190        4250
    #> # … with 334 more rows, and 2 more variables: sex <fct>, year <int>
    
    
    penguins %>% 
        group_by(species, island) %>% 
        group_by(
            across(-c(
                starts_with("bill"),
                starts_with("flipper"),
                starts_with("body")
            )),
            species # "continue" to use species for grouping
        )
    #> # A tibble: 344 x 8
    #> # Groups:   sex, year, species [22]
    #>    species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g
    #>    <fct>   <fct>           <dbl>         <dbl>            <int>       <int>
    #>  1 Adelie  Torge…           39.1          18.7              181        3750
    #>  2 Adelie  Torge…           39.5          17.4              186        3800
    #>  3 Adelie  Torge…           40.3          18                195        3250
    #>  4 Adelie  Torge…           NA            NA                 NA          NA
    #>  5 Adelie  Torge…           36.7          19.3              193        3450
    #>  6 Adelie  Torge…           39.3          20.6              190        3650
    #>  7 Adelie  Torge…           38.9          17.8              181        3625
    #>  8 Adelie  Torge…           39.2          19.6              195        4675
    #>  9 Adelie  Torge…           34.1          18.1              193        3475
    #> 10 Adelie  Torge…           42            20.2              190        4250
    #> # … with 334 more rows, and 2 more variables: sex <fct>, year <int>
    

    reprex package (v0.3.0) 于 2020 年 9 月 7 日创建

    sessionInfo()
    #> R version 4.0.2 (2020-06-22)
    #> Platform: x86_64-pc-linux-gnu (64-bit)
    #> Running under: Ubuntu 20.04.1 LTS
    #> 
    #> Matrix products: default
    #> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
    #> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3
    #> 
    #> locale:
    #>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
    #>  [3] LC_TIME=it_IT.UTF-8        LC_COLLATE=en_US.UTF-8    
    #>  [5] LC_MONETARY=it_IT.UTF-8    LC_MESSAGES=en_US.UTF-8   
    #>  [7] LC_PAPER=it_IT.UTF-8       LC_NAME=C                 
    #>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
    #> [11] LC_MEASUREMENT=it_IT.UTF-8 LC_IDENTIFICATION=C       
    #> 
    #> attached base packages:
    #> [1] stats     graphics  grDevices datasets  utils     methods   base     
    #> 
    #> other attached packages:
    #>  [1] palmerpenguins_0.1.0 forcats_0.5.0        stringr_1.4.0       
    #>  [4] dplyr_1.0.2          purrr_0.3.4          readr_1.3.1         
    #>  [7] tidyr_1.1.2          tibble_3.0.3         ggplot2_3.3.2       
    #> [10] tidyverse_1.3.0     
    #> 
    #> loaded via a namespace (and not attached):
    #>  [1] tidyselect_1.1.0 xfun_0.16        haven_2.3.1      colorspace_1.4-1
    #>  [5] vctrs_0.3.4      generics_0.0.2   htmltools_0.5.0  yaml_2.2.1      
    #>  [9] utf8_1.1.4       blob_1.2.1       rlang_0.4.7      pillar_1.4.6    
    #> [13] glue_1.4.2       withr_2.2.0      DBI_1.1.0        dbplyr_1.4.4    
    #> [17] modelr_0.1.8     readxl_1.3.1     lifecycle_0.2.0  munsell_0.5.0   
    #> [21] gtable_0.3.0     cellranger_1.1.0 rvest_0.3.6      evaluate_0.14   
    #> [25] knitr_1.29       fansi_0.4.1      highr_0.8        broom_0.7.0     
    #> [29] Rcpp_1.0.5       renv_0.12.0      scales_1.1.1     backports_1.1.9 
    #> [33] jsonlite_1.7.0   fs_1.5.0         hms_0.5.3        digest_0.6.25   
    #> [37] stringi_1.4.6    grid_4.0.2       cli_2.0.2        tools_4.0.2     
    #> [41] magrittr_1.5     crayon_1.3.4     pkgconfig_2.0.3  ellipsis_0.3.1  
    #> [45] xml2_1.3.2       reprex_0.3.0     lubridate_1.7.9  assertthat_0.2.1
    #> [49] rmarkdown_2.3    httr_1.4.2       R6_2.4.1         compiler_4.0.2
    

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 2023-02-03
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2021-06-10
      • 2013-12-08
      • 2021-11-27
      • 1970-01-01
      相关资源
      最近更新 更多