dplyr 中 rename_with 函数重命名后产生意外输出

3
这个问题与r重命名列名以包含0有关,基本上最后一位数字1到9应该被重命名为01-09: Ronak Shah提供的解决方案如下:
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('%02s', 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

使用相同的数据和代码,我得到了一个空格而不是零:
  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

我的会话信息:

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    

什么原因会导致这种行为?

3
可能应该使用 %02d 而不是 %02s,因此您可能还需要使用 as.numeric(m) - walter
它可以工作,但另一方面为什么%02s与Ronak Shah的解决方案一起工作。 - TarJae
1
奇怪。唯一的区别是我使用的是 R 4.1.0 版本。对于你来说,sprintf('%02s', 1)sprintf('%02s', '1') 的输出是什么?对我来说,两者都是 "01"。 - Ronak Shah
sprintf('%02s', 1) sprintf('%02s', '1')gives: `> sprintf('%02s', 1) [1] " 1"
sprintf('%02s', '1') [1] " 1"`
- TarJae
1个回答

1

可以使用str_pad函数,它不需要区分字符和数字。

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

在 macOS 上的 R 4.1.1 中,OP 的代码可以正常工作。
df %>%
  rename_with(~str_replace(., '\\d+', function(m) sprintf('%02s', 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

非常感谢。但是为什么这段代码在另一台机器上可以运行,而在我的机器上却不能呢?可能是选项有所改变吗?我今天一直在查找,但我无法解释为什么我没有得到零。 - TarJae
1
@TarJae,我在R 4.1.1的新闻中发现,“解析包含UTF-16代理对(例如“\uD834\uDD1E”)的字符串在某些(不常见的)平台上效果更好。sprintf("%X",utf8ToInt("\uD834\uDD1E"))现在应该在所有平台上都能给出“1D11E”。我猜这可能是一个错误。也许你需要报告一下以修复它。 - akrun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接