Thanks to visit codestin.com
Credit goes to github.com

Skip to content

recode_char incorrectly replaces values when regex and default are both set #654

@alinacherkas

Description

@alinacherkas

Describe the bug

When using recode_char with regex set to TRUE and default value specified, the function incorrectly handles replacements. It seems that the issue is related to indices and how default value is handled internally. The issue was originally found in 2.0.15 but is still there in 2.0.17.

Steps/Code to Reproduce

x <- c("-1", "1", "0", "2", "-2")
collapse::recode_char(x, "-\\d+" = "negative", "0" = "zero", default = "positive", regex = T)
# [1] "positive" "positive" "zero" "positive" "positive"

Expected Results

x <- c("-1", "1", "0", "2", "-2")
collapse::recode_char(x, "-\\d+" = "negative", "0" = "zero", regex = T)
# [1] "negative" "1" "zero" "2" "negative"
collapse::recode_char(x, "0" = "zero", "-\\d+" = "negative", default = "positive", regex = T)
# [1] "negative" "positive" "zero" "positive" "negative"
collapse::recode_char(x, "-\\d+" = "negative", "0" = "zero", default = "positive", regex = T)
# [1] "negative" "positive" "zero" "positive" "negative"

Actual Results

x <- c("-1", "1", "0", "2", "-2")
collapse::recode_char(x, "-\\d+" = "negative", "0" = "zero", regex = T)
# [1] "negative" "1" "zero" "2" "negative"
collapse::recode_char(x, "0" = "zero", "-\\d+" = "negative", default = "positive", regex = T)
# [1] "negative" "positive" "zero" "positive" "negative"
collapse::recode_char(x, "-\\d+" = "negative", "0" = "zero", default = "positive", regex = T)
# [1] "positive" "positive" "zero" "positive" "positive"

Session Info

> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.0.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.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: ...
tzcode source: internal

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

other attached packages:
[1] collapse_2.0.17  testthat_3.2.1.1

loaded via a namespace (and not attached):
 [1] miniUI_0.1.1.1    compiler_4.3.2    brio_1.1.5        promises_1.3.0    Rcpp_1.0.12       stringr_1.5.1     parallel_4.3.2    later_1.3.2      
 [9] fastmap_1.1.1     mime_0.12         R6_2.5.1          htmlwidgets_1.6.4 tibble_3.2.1      desc_1.4.3        profvis_0.3.8     rprojroot_2.0.4  
[17] shiny_1.8.1.1     pillar_1.9.0      rlang_1.1.3       utf8_1.2.4        cachem_1.0.8      stringi_1.8.3     httpuv_1.6.15     fs_1.6.4         
[25] pkgload_1.3.4     memoise_2.0.1     cli_3.6.2         withr_3.0.0       magrittr_2.0.3    digest_0.6.35     rstudioapi_0.16.0 xtable_1.8-4     
[33] remotes_2.5.0     devtools_2.4.5    lifecycle_1.0.4   waldo_0.5.2       vctrs_0.6.5       glue_1.7.0        urlchecker_1.0.1  sessioninfo_1.2.2
[41] pkgbuild_1.4.4    fansi_1.0.6       purrr_1.0.2       pkgconfig_2.0.3   tools_4.3.2       usethis_2.2.3     ellipsis_0.3.2    htmltools_0.5.8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions