It works alone, but fails successively

library(tidyverse)
library(palmerpenguins)

# code A
penguins %>%
  group_by(species, island) %>%
  summarise(
    prob = c(.25, .75),
    across(
      c(bill_length_mm, bill_depth_mm, flipper_length_mm),
      ~ quantile(., prob, na.rm = TRUE)
    )
  )



# code B
penguins %>%
  group_by(species) %>%
  summarise(
    n = n(),
    across(starts_with("bill_"), mean, na.rm = TRUE),
    Area = mean(bill_length_mm * bill_depth_mm, na.rm = TRUE),
    across(ends_with("_g"), mean, na.rm = TRUE),
  )

It works alone, but fails successively

  • code A, work
  • code B, work
  • code A + code B, error

I can't reproduce this. It works fine for me. What error are you getting? Could you share your output?

library(tidyverse)
library(palmerpenguins)

# code A
penguins %>%
  group_by(species, island) %>%
  summarise(
    prob = c(.25, .75),
    across(
      c(bill_length_mm, bill_depth_mm, flipper_length_mm),
      ~ quantile(., prob, na.rm = TRUE)
    )
  )
#> `summarise()` regrouping output by 'species', 'island' (override with `.groups` argument)
#> # A tibble: 10 x 6
#> # Groups:   species, island [5]
#>    species   island     prob bill_length_mm bill_depth_mm flipper_length_mm
#>    <fct>     <fct>     <dbl>          <dbl>         <dbl>             <dbl>
#>  1 Adelie    Biscoe     0.25           37.7          17.6              185.
#>  2 Adelie    Biscoe     0.75           40.7          19.0              193 
#>  3 Adelie    Dream      0.25           36.8          17.5              185 
#>  4 Adelie    Dream      0.75           40.4          18.8              193 
#>  5 Adelie    Torgersen  0.25           36.7          17.4              187 
#>  6 Adelie    Torgersen  0.75           41.1          19.2              195 
#>  7 Chinstrap Dream      0.25           46.3          17.5              191 
#>  8 Chinstrap Dream      0.75           51.1          19.4              201 
#>  9 Gentoo    Biscoe     0.25           45.3          14.2              212 
#> 10 Gentoo    Biscoe     0.75           49.6          15.7              221



# code B
penguins %>%
  group_by(species) %>%
  summarise(
    n = n(),
    across(starts_with("bill_"), mean, na.rm = TRUE),
    Area = mean(bill_length_mm * bill_depth_mm, na.rm = TRUE),
    across(ends_with("_g"), mean, na.rm = TRUE),
  )
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 6
#>   species       n bill_length_mm bill_depth_mm  Area body_mass_g
#>   <fct>     <int>          <dbl>         <dbl> <dbl>       <dbl>
#> 1 Adelie      152           38.8          18.3  712.       3701.
#> 2 Chinstrap    68           48.8          18.4  900.       3733.
#> 3 Gentoo      124           47.5          15.0  712.       5076.

Created on 2021-02-03 by the reprex package (v0.3.0)

library(tidyverse)
library(palmerpenguins)

# code A
penguins %>%
  group_by(species, island) %>%
  summarise(
    prob = c(.25, .75),
    across(
      c(bill_length_mm, bill_depth_mm, flipper_length_mm),
      ~ quantile(., prob, na.rm = TRUE)
    )
  )
#> `summarise()` has grouped output by 'species', 'island'. You can override using the `.groups` argument.
#> # A tibble: 10 x 6
#> # Groups:   species, island [5]
#>    species   island     prob bill_length_mm bill_depth_mm flipper_length_mm
#>    <fct>     <fct>     <dbl>          <dbl>         <dbl>             <dbl>
#>  1 Adelie    Biscoe     0.25           37.7          17.6              185.
#>  2 Adelie    Biscoe     0.75           40.7          19.0              193 
#>  3 Adelie    Dream      0.25           36.8          17.5              185 
#>  4 Adelie    Dream      0.75           40.4          18.8              193 
#>  5 Adelie    Torgersen  0.25           36.7          17.4              187 
#>  6 Adelie    Torgersen  0.75           41.1          19.2              195 
#>  7 Chinstrap Dream      0.25           46.3          17.5              191 
#>  8 Chinstrap Dream      0.75           51.1          19.4              201 
#>  9 Gentoo    Biscoe     0.25           45.3          14.2              212 
#> 10 Gentoo    Biscoe     0.75           49.6          15.7              221



# code B
penguins %>%
  group_by(species) %>%
  summarise(
    n = n(),
    across(starts_with("bill_"), mean, na.rm = TRUE),
    Area = mean(bill_length_mm * bill_depth_mm, na.rm = TRUE),
    across(ends_with("_g"), mean, na.rm = TRUE),
  )
#> Error: Can't subset elements that don't exist.
#> x Location 5 doesn't exist.
#> i There are only 3 elements.


sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19041)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=Chinese (Simplified)_China.936 
#> [2] LC_CTYPE=Chinese (Simplified)_China.936   
#> [3] LC_MONETARY=Chinese (Simplified)_China.936
#> [4] LC_NUMERIC=C                              
#> [5] LC_TIME=Chinese (Simplified)_China.936    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] palmerpenguins_0.1.0 forcats_0.5.1        stringr_1.4.0       
#>  [4] dplyr_1.0.4          purrr_0.3.4          readr_1.4.0         
#>  [7] tidyr_1.1.2          tibble_3.0.6         ggplot2_3.3.3       
#> [10] tidyverse_1.3.0     
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.6        cellranger_1.1.0  pillar_1.4.7      compiler_4.0.2   
#>  [5] dbplyr_2.0.0      highr_0.8         tools_4.0.2       digest_0.6.27    
#>  [9] lubridate_1.7.9.2 jsonlite_1.7.2    evaluate_0.14     lifecycle_0.2.0  
#> [13] gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.10      reprex_1.0.0     
#> [17] cli_2.3.0         DBI_1.1.1         yaml_2.2.1        haven_2.3.1      
#> [21] xfun_0.20         withr_2.4.1       xml2_1.3.2        httr_1.4.2       
#> [25] styler_1.3.2      knitr_1.31        hms_1.0.0         generics_0.1.0   
#> [29] fs_1.5.0          vctrs_0.3.6       grid_4.0.2        tidyselect_1.1.0 
#> [33] glue_1.4.2        R6_2.5.0          fansi_0.4.1       readxl_1.3.1     
#> [37] rmarkdown_2.6     modelr_0.1.8      magrittr_2.0.1    backports_1.2.1  
#> [41] scales_1.1.1      ellipsis_0.3.1    htmltools_0.5.1.1 rvest_0.3.6      
#> [45] assertthat_0.2.1  colorspace_2.0-0  utf8_1.1.4        stringi_1.5.3    
#> [49] munsell_0.5.0     broom_0.7.4       crayon_1.4.0

Created on 2021-02-03 by the reprex package (v1.0.0)

I update dplyr to dplyr_1.0.4

This is very strange! After I updated, I can reproduce the same issue

library(tidyverse)
library(palmerpenguins)

# code A
penguins %>%
  group_by(species, island) %>%
  summarise(
    prob = c(.25, .75),
    across(
      c(bill_length_mm, bill_depth_mm, flipper_length_mm),
      ~ quantile(., prob, na.rm = TRUE)
    )
  )
#> `summarise()` has grouped output by 'species', 'island'. You can override using the `.groups` argument.
#> # A tibble: 10 x 6
#> # Groups:   species, island [5]
#>    species   island     prob bill_length_mm bill_depth_mm flipper_length_mm
#>    <fct>     <fct>     <dbl>          <dbl>         <dbl>             <dbl>
#>  1 Adelie    Biscoe     0.25           37.7          17.6              185.
#>  2 Adelie    Biscoe     0.75           40.7          19.0              193 
#>  3 Adelie    Dream      0.25           36.8          17.5              185 
#>  4 Adelie    Dream      0.75           40.4          18.8              193 
#>  5 Adelie    Torgersen  0.25           36.7          17.4              187 
#>  6 Adelie    Torgersen  0.75           41.1          19.2              195 
#>  7 Chinstrap Dream      0.25           46.3          17.5              191 
#>  8 Chinstrap Dream      0.75           51.1          19.4              201 
#>  9 Gentoo    Biscoe     0.25           45.3          14.2              212 
#> 10 Gentoo    Biscoe     0.75           49.6          15.7              221

# code B
penguins %>%
  group_by(species) %>%
  summarise(
    n = n(),
    across(starts_with("bill_"), mean, na.rm = TRUE),
    Area = mean(bill_length_mm * bill_depth_mm, na.rm = TRUE),
    across(ends_with("_g"), mean, na.rm = TRUE),
  )
#> Error: Can't subset elements that don't exist.
#> x Location 5 doesn't exist.
#> i There are only 3 elements.

Created on 2021-02-03 by the reprex package (v1.0.0)

library(tidyverse)
library(palmerpenguins)

# code B
penguins %>%
  group_by(species) %>%
  summarise(
    n = n(),
    across(starts_with("bill_"), mean, na.rm = TRUE),
    Area = mean(bill_length_mm * bill_depth_mm, na.rm = TRUE),
    across(ends_with("_g"), mean, na.rm = TRUE),
  )
#> # A tibble: 3 x 6
#>   species       n bill_length_mm bill_depth_mm  Area body_mass_g
#> * <fct>     <int>          <dbl>         <dbl> <dbl>       <dbl>
#> 1 Adelie      152           38.8          18.3  712.       3701.
#> 2 Chinstrap    68           48.8          18.4  900.       3733.
#> 3 Gentoo      124           47.5          15.0  712.       5076.

# code A
penguins %>%
  group_by(species, island) %>%
  summarise(
    prob = c(.25, .75),
    across(
      c(bill_length_mm, bill_depth_mm, flipper_length_mm),
      ~ quantile(., prob, na.rm = TRUE)
    )
  )
#> `summarise()` has grouped output by 'species', 'island'. You can override using the `.groups` argument.
#> # A tibble: 10 x 6
#> # Groups:   species, island [5]
#>    species   island     prob bill_length_mm bill_depth_mm flipper_length_mm
#>    <fct>     <fct>     <dbl>          <dbl>         <dbl>             <dbl>
#>  1 Adelie    Biscoe     0.25           37.7          17.6              185.
#>  2 Adelie    Biscoe     0.75           40.7          19.0              193 
#>  3 Adelie    Dream      0.25           36.8          17.5              185 
#>  4 Adelie    Dream      0.75           40.4          18.8              193 
#>  5 Adelie    Torgersen  0.25           36.7          17.4              187 
#>  6 Adelie    Torgersen  0.75           41.1          19.2              195 
#>  7 Chinstrap Dream      0.25           46.3          17.5              191 
#>  8 Chinstrap Dream      0.75           51.1          19.4              201 
#>  9 Gentoo    Biscoe     0.25           45.3          14.2              212 
#> 10 Gentoo    Biscoe     0.75           49.6          15.7              221

Created on 2021-02-03 by the reprex package (v1.0.0)

I created an issue on GitHub: https://github.com/tidyverse/dplyr/issues/5733

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.