Home > Enterprise >  Unexpected output after rename_with dplyr
Unexpected output after rename_with dplyr

Time:10-11

This question is related to r rename column names to include 0s, where basically the last one digit number 1 to 9 should be renamed with 01-09:

The solution provided by Ronak Shah gives this:

df <- structure(list(Id = c(1L, 2L, 1L, 4L), Date = c("02/19/2020", 
"02/10/2020", "03/11/2020", "10/29/2020"), Col_a_1 = c(0L, 1L, 
2L, 1L), Col_a_2 = c(1L, 2L, 1L, 0L), Col_a_3 = c(2L, 0L, 3L, 
2L), Col_a_12 = c(0L, 1L, 1L, 1L), Col_a_65 = c(4L, 3L, 0L, 0L
)), class = "data.frame", row.names = c(NA, -4L))

library(dplyr)
library(stringr)

df %>%
  rename_with(~str_replace(., '\\d ', function(m) sprintf('s', m)), 
              starts_with('Col'))

#  Id       Date Col_a_01 Col_a_02 Col_a_03 Col_a_12 Col_a_65
#1  1 02/19/2020        0        1        2        0        4
#2  2 02/10/2020        1        2        0        1        3
#3  1 03/11/2020        2        1        3        1        0
#4  4 10/29/2020        1        0        2        1        0

With the same data and the same code I get a space instead of the zero:

  Id       Date Col_a_ 1 Col_a_ 2 Col_a_ 3 Col_a_12 Col_a_65
1  1 02/19/2020        0        1        2        0        4
2  2 02/10/2020        1        2        0        1        3
3  1 03/11/2020        2        1        3        1        0
4  4 10/29/2020        1        0        2        1        0

My session info:

sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

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

other attached packages:
[1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7     purrr_0.3.4     readr_2.0.2    
[6] tidyr_1.1.4     tibble_3.1.5    ggplot2_3.3.5   tidyverse_1.3.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7       cellranger_1.1.0 pillar_1.6.3     compiler_4.1.1  
 [5] dbplyr_2.1.1     tools_4.1.1      jsonlite_1.7.2   lubridate_1.8.0 
 [9] lifecycle_1.0.1  gtable_0.3.0     pkgconfig_2.0.3  rlang_0.4.11    
[13] reprex_2.0.1     cli_3.0.1        rstudioapi_0.13  DBI_1.1.1       
[17] haven_2.4.3      xml2_1.3.2       withr_2.4.2      httr_1.4.2      
[21] fs_1.5.0         generics_0.1.0   vctrs_0.3.8      hms_1.1.1       
[25] grid_4.1.1       tidyselect_1.1.1 glue_1.4.2       R6_2.5.1        
[29] fansi_0.5.0      readxl_1.3.1     tzdb_0.1.2       modelr_0.1.8    
[33] magrittr_2.0.1   backports_1.2.1  scales_1.1.1     ellipsis_0.3.2  
[37] rvest_1.0.1      assertthat_0.2.1 colorspace_2.0-2 utf8_1.2.2      
[41] stringi_1.7.5    munsell_0.5.0    broom_0.7.9      crayon_1.4.1    

What can be the reason for this behaviour?

CodePudding user response:

May be use str_pad which doesn't have to distinguish between character and numeric

library(dplyr)
library(stringr)
df %>% 
   rename_with(~ str_replace(., '\\d ', function(m) str_pad(m, width = 2, pad = '0')))
  Id       Date Col_a_01 Col_a_02 Col_a_03 Col_a_12 Col_a_65
1  1 02/19/2020        0        1        2        0        4
2  2 02/10/2020        1        2        0        1        3
3  1 03/11/2020        2        1        3        1        0
4  4 10/29/2020        1        0        2        1        0

In R 4.1.1 on macOS the OP's code works though

df %>%
  rename_with(~str_replace(., '\\d ', function(m) sprintf('s', m)), 
              starts_with('Col'))
   Id       Date Col_a_01 Col_a_02 Col_a_03 Col_a_12 Col_a_65
1  1 02/19/2020        0        1        2        0        4
2  2 02/10/2020        1        2        0        1        3
3  1 03/11/2020        2        1        3        1        0
4  4 10/29/2020        1        0        2        1        0
  • Related