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

Incorrect order of colnames in pivot_wider.() with names_glue #579

Closed
Darxor opened this issue Sep 5, 2022 · 1 comment · Fixed by #580
Closed

Incorrect order of colnames in pivot_wider.() with names_glue #579

Darxor opened this issue Sep 5, 2022 · 1 comment · Fixed by #580
Labels
bug Something isn't working

Comments

@Darxor
Copy link
Contributor

Darxor commented Sep 5, 2022

pivot_wider.() assigns names to variables in incorrect order, when:

  1. names_glue parameter is used with {.value} placeholder
  2. variable is names_from is unsorted

Error is reproducible in both current CRAN version and development version.
See minimal example below. With names_glue (df2) columns are ordered as they would be without this option (df1), but column names are ordered as values appear in the lettr column. This issue is solved, when letter variable is sorted beforehand (df3).

set.seed(1)
src <- tidytable::tidytable(
  lettr = sample(letters, size = 5),
  v1 = 1:5
)

# correct
(df1 <- tidytable::pivot_wider.(
    src,
    names_from = lettr,
    values_from = v1
  ))
#> # A tidytable: 1 × 5
#>       a     b     d     g     y
#>   <int> <int> <int> <int> <int>
#> 1     4     5     2     3     1

# incorrect
(df2 <- tidytable::pivot_wider.(
  src,
  names_from = lettr,
  values_from = v1,
  names_glue = "{.value}_{lettr}"
))
#> # A tidytable: 1 × 5
#>    v1_y  v1_d  v1_g  v1_a  v1_b
#>   <int> <int> <int> <int> <int>
#> 1     4     5     2     3     1

# correct
(df2_2 <- tidytable::pivot_wider.(
  tidytable::arrange.(src, lettr),
  names_from = lettr,
  values_from = v1,
  names_glue = "{.value}_{lettr}"
))
#> # A tidytable: 1 × 5
#>    v1_a  v1_b  v1_d  v1_g  v1_y
#>   <int> <int> <int> <int> <int>
#> 1     4     5     2     3     1

Created on 2022-09-05 with reprex v2.0.2

Session info
> sessioninfo::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23 ucrt)
 os       Windows 10 x64 (build 22000)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  Russian_Russia.utf8
 ctype    Russian_Russia.utf8
 tz       Europe/Moscow
 date     2022-09-05
 rstudio  2022.07.0+548 Spotted Wakerobin (desktop)
 pandoc   2.18 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 callr         3.7.2   2022-08-22 [1] CRAN (R 4.2.1)
 cli           3.3.0   2022-04-25 [1] CRAN (R 4.2.0)
 clipr         0.8.0   2022-02-22 [1] CRAN (R 4.2.0)
 crayon        1.5.1   2022-03-26 [1] CRAN (R 4.2.0)
 curl          4.3.2   2021-06-23 [1] CRAN (R 4.2.0)
 data.table    1.14.2  2021-09-27 [1] CRAN (R 4.2.0)
 digest        0.6.29  2021-12-01 [1] CRAN (R 4.2.0)
 ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.2.0)
 evaluate      0.16    2022-08-09 [1] CRAN (R 4.2.1)
 fansi         1.0.3   2022-03-24 [1] CRAN (R 4.2.0)
 fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.0)
 fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.0)
 glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
 highr         0.9     2021-04-16 [1] CRAN (R 4.2.0)
 htmltools     0.5.3   2022-07-18 [1] CRAN (R 4.2.1)
 knitr         1.40    2022-08-24 [1] CRAN (R 4.2.1)
 lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.2.0)
 magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
 pillar        1.8.1   2022-08-19 [1] CRAN (R 4.2.1)
 pkgbuild      1.3.1   2021-12-20 [1] CRAN (R 4.2.0)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
 prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.2.0)
 processx      3.7.0   2022-07-07 [1] CRAN (R 4.2.1)
 ps            1.7.1   2022-06-18 [1] CRAN (R 4.2.0)
 purrr         0.3.4   2020-04-17 [1] CRAN (R 4.2.0)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
 remotes       2.4.2   2021-11-30 [1] CRAN (R 4.2.0)
 reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.1)
 rlang         1.0.5   2022-08-31 [1] CRAN (R 4.2.1)
 rmarkdown     2.16    2022-08-24 [1] CRAN (R 4.2.1)
 rprojroot     2.0.3   2022-04-02 [1] CRAN (R 4.2.0)
 rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.1)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
 tibble        3.1.8   2022-07-22 [1] CRAN (R 4.2.1)
 tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.2.0)
 tidytable     0.8.1.9 2022-09-05 [1] Github (markfairbanks/tidytable@5267e8a)
 utf8          1.2.2   2021-07-24 [1] CRAN (R 4.2.0)
 vctrs         0.4.1   2022-04-13 [1] CRAN (R 4.2.0)
 withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
 xfun          0.32    2022-08-10 [1] CRAN (R 4.2.1)
 yaml          2.3.5   2022-02-21 [1] CRAN (R 4.2.0)

 [1] C:/Users/Darx/AppData/Local/R/win-library/4.2
 [2] C:/Program Files/R/R-4.2.1/library

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
@Darxor
Copy link
Contributor Author

Darxor commented Sep 5, 2022

The issue is also non-existant with names_sort = TRUE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants