【问题标题】:Unexpected output after rename_with dplyrrename_with dplyr 后的意外输出
【发布时间】:2021-11-29 10:08:44
【问题描述】:

这个问题与r rename column names to include 0s有关,基本上最后一位数字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    

这种行为的原因是什么?

【问题讨论】:

  • 可能应该是 %02d 而不是 %02s,所以你可能还需要as.numeric(m)
  • 它有效,但为什么另一方面 %02s 与 Ronak Shah 的解决方案一起工作
  • 很奇怪。唯一的区别是我使用的是 R 4.1.0。 sprintf('%02s', 1)sprintf('%02s', '1') 的输出是什么?我得到了他们两个的“01”。
  • sprintf('%02s', 1) sprintf('%02s', '1')gives: &gt; sprintf('%02s', 1) [1] " 1" &gt; sprintf('%02s', '1') [1] " 1"

标签: r dplyr rename str-replace


【解决方案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

【讨论】:

  • 非常感谢。但是此代码:df %&gt;% rename_with(~str_replace(., '\\d+', function(m) sprintf('%02s', m)), starts_with('Col')) 在另一台机器上工作但在我的机器上不起作用的原因可能是什么?会不会是选项的改变。我今天一直在经历,我无法解释为什么我没有得到零。
  • @TarJae 我在 R 4.1.1 的新闻中发现 ParsingstringscontainingUTF-16surrogatepairssuchas"\uD834\uDD1E"worksbetteronsome(uncommon)platforms.sprintf("%X",utf8ToInt("\uD834\uDD1E"))should now give"1D11E"on allplatforms.。我猜这可能是一个错误。也许你需要报告这个来解决它
猜你喜欢
  • 2018-08-01
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2021-10-13
  • 2020-02-21
  • 2012-05-06
相关资源
最近更新 更多