Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple across calls to arrange ignore subsequent across statements if first returns more than one column #6490

Closed
andrewbaxter439 opened this issue Oct 4, 2022 · 2 comments · Fixed by #6491

Comments

@andrewbaxter439
Copy link

andrewbaxter439 commented Oct 4, 2022

As noted in this stack overflow question, using across(starts_with(...)) in an arrange() call has odd behaviour when selecting more than one column.

See example below with two across calls looking for one or more columns. If the first across call returns more than one column, then these are ordered with the expected priority, but the second call to across is ignored. If the first across call returns one column, then the second evaluates fine.

library(tibble)
library(dplyr)

df <- tibble(a_1 = rep(3:1, each = 4),
             a_2 = rep(13:11, each = 4),
             b_1 = rep(24:21, each = 3),
             b_2 = 22:11)

df |> 
  arrange(across(starts_with("a_")),
          across(starts_with("b_")))

#> # A tibble: 12 × 4
#>      a_1   a_2   b_1   b_2
#>    <int> <int> <int> <int>
#>  1     1    11    22    14
#>  2     1    11    21    13
#>  3     1    11    21    12
#>  4     1    11    21    11
#>  5     2    12    23    18
#>  6     2    12    23    17
#>  7     2    12    22    16
#>  8     2    12    22    15
#>  9     3    13    24    22
#> 10     3    13    24    21
#> 11     3    13    24    20
#> 12     3    13    23    19

# `a_1` and `a_2` are correctly ordered; `b_1` and `b_2` have remained untouched within each stratum of `a_1`*`a_2`

df |> 
  select(-a_2) |> 
  arrange(across(starts_with("a_")),
          across(starts_with("b_")))

#> # A tibble: 12 × 3
#>      a_1   b_1   b_2
#>    <int> <int> <int>
#>  1     1    21    11
#>  2     1    21    12
#>  3     1    21    13
#>  4     1    22    14
#>  5     2    22    15
#>  6     2    22    16
#>  7     2    23    17
#>  8     2    23    18
#>  9     3    23    19
#> 10     3    24    20
#> 11     3    24    21
#> 12     3    24    22

# `a_1`, `b_1` and `b_2` are now correctly ordered.
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.1 (2022-06-23 ucrt)
#>  os       Windows 10 x64 (build 19044)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United Kingdom.utf8
#>  ctype    English_United Kingdom.utf8
#>  tz       Europe/London
#>  date     2022-10-04
#>  pandoc   2.18 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.1)
#>  cli           3.3.0   2022-04-25 [1] CRAN (R 4.1.1)
#>  crayon        1.5.1   2022-03-26 [1] CRAN (R 4.1.3)
#>  DBI           1.1.3   2022-06-18 [1] CRAN (R 4.2.1)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.1.3)
#>  dplyr       * 1.0.9   2022-04-28 [1] CRAN (R 4.1.1)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.1)
#>  evaluate      0.15    2022-02-18 [1] CRAN (R 4.1.3)
#>  fansi         1.0.3   2022-03-24 [1] CRAN (R 4.1.3)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.1.1)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.1.3)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.0)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.1.3)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.1)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.1)
#>  knitr         1.39    2022-04-26 [1] CRAN (R 4.1.3)
#>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.1)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.1.3)
#>  pillar        1.7.0   2022-02-01 [1] CRAN (R 4.1.3)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.1)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.1)
#>  R.cache       0.15.0  2021-04-30 [1] CRAN (R 4.1.3)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.0  2022-06-28 [1] CRAN (R 4.2.1)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.1)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.1)
#>  rlang         1.0.3   2022-06-27 [1] CRAN (R 4.2.1)
#>  rmarkdown     2.14    2022-04-25 [1] CRAN (R 4.1.3)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.3)
#>  stringi       1.7.8   2022-07-11 [1] CRAN (R 4.2.1)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.1)
#>  styler        1.7.0   2022-03-13 [1] CRAN (R 4.1.3)
#>  tibble      * 3.1.7   2022-05-03 [1] CRAN (R 4.2.1)
#>  tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.1.3)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.1)
#>  vctrs         0.4.1   2022-04-13 [1] CRAN (R 4.1.3)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.1.3)
#>  xfun          0.31    2022-05-10 [1] CRAN (R 4.2.1)
#>  yaml          2.3.5   2022-02-21 [1] CRAN (R 4.1.2)
#> 
#>  [1] C:/R Packages
#>  [2] C:/Program Files/R/R-4.2.1/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@DavisVaughan
Copy link
Member

Already fixed in the dev version!

library(dplyr)

df <- tibble(a_1 = rep(3:1, each = 4),
             a_2 = rep(13:11, each = 4),
             b_1 = rep(24:21, each = 3),
             b_2 = 22:11)

df |> 
  arrange(across(starts_with("a_")),
          across(starts_with("b_")))
#> # A tibble: 12 × 4
#>      a_1   a_2   b_1   b_2
#>    <int> <int> <int> <int>
#>  1     1    11    21    11
#>  2     1    11    21    12
#>  3     1    11    21    13
#>  4     1    11    22    14
#>  5     2    12    22    15
#>  6     2    12    22    16
#>  7     2    12    23    17
#>  8     2    12    23    18
#>  9     3    13    23    19
#> 10     3    13    24    20
#> 11     3    13    24    21
#> 12     3    13    24    22

@andrewbaxter439
Copy link
Author

Great, thanks for this! Had searched issues but hadn't seen the linked one, nor would have seen the connection if I had. Works perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants