stringr icon indicating copy to clipboard operation
stringr copied to clipboard

str_sort drops vector names attribute

Open warnes opened this issue 1 year ago • 2 comments

With stringr 1.5.1, the str_sort function strips names from the vector.

Current behavior:

x <- c(A="3", B="2", C="1")
> str(x)
 Named chr [1:3] "3" "2" "1"
 - attr(*, "names")= chr [1:3] "A" "B" "C"
> stringr::str_sort(x)
[1] "1" "2" "3"
> str(stringr::str_sort(x))
 chr [1:3] "1" 

Expected behavior:

x <- c(A="3", B="2", C="1")
> str(x)
 Named chr [1:3] "3" "2" "1"
 - attr(*, "names")= chr [1:3] "A" "B" "C"
> stringr::str_sort(x)
[1] "1" "2" "3"
> str(stringr::str_sort(x))
 Named chr [1:3] "1" "2" "3"
 - attr(*, "names")= chr [1:3] "C" "B" "A"

> sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: x86_64-apple-darwin20
Running under: macOS 15.0.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] devtools_2.4.5 usethis_2.2.3

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       cli_3.6.2         rlang_1.1.4       stringi_1.8.4
 [5] purrr_1.0.2       pkgload_1.3.4     renv_1.0.11       promises_1.3.0
 [9] shiny_1.8.1.1     xtable_1.8-4      glue_1.7.0        htmltools_0.5.8.1
[13] httpuv_1.6.15     pkgbuild_1.4.4    ellipsis_0.3.2    fastmap_1.2.0
[17] lifecycle_1.0.4   memoise_2.0.1     stringr_1.5.1     compiler_4.4.1
[21] miniUI_0.1.1.1    sessioninfo_1.2.2 fs_1.6.4          htmlwidgets_1.6.4
[25] Rcpp_1.0.12       urlchecker_1.0.1  later_1.3.2       tcltk_4.4.1
[29] digest_0.6.36     R6_2.5.1          magrittr_2.0.3    tools_4.4.1
[33] mime_0.12         profvis_0.3.8     remotes_2.5.0     cachem_1.1.0

warnes avatar Nov 01 '24 10:11 warnes

Same for str_replace_all(). This was fixed recently for str_subset() in https://github.com/tidyverse/stringr/pull/560 , and I would like to see it everywhere it makes sense.

> letters |> rlang::set_names() |> stringr::str_subset("[ae]")
  a   e 
"a" "e" 
> letters |> rlang::set_names() |> stringr::str_replace_all("a", "e")
 [1] "e" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"

jonovik avatar Nov 19 '24 14:11 jonovik

same issue for arbitrary attributes:

c("a", "b") |> 
    structure(test = "hi") |> 
    attr("test")
#> [1] "hi"

# attribute lost after `str_replace()`:
c("a", "b") |> 
    structure(test = "hi") |> 
    stringr::str_replace("b", "c") |>
    attr("test")
#> NULL

Created on 2025-01-28 with reprex v2.1.1

(My actual use case is the label attribute from e.g. {labelled})

d-morrison avatar Jan 29 '25 07:01 d-morrison

Since all these functions are thin wrappers around stringi functions, stringi would be the place to file these issues.

hadley avatar Aug 15 '25 12:08 hadley