r 重命名列名以包含 0

r rename column names to include 0s

这是我的数据集

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

我喜欢给那些以 1,2,3 结尾的列名加 0

最终预期的数据集是具有这些列名的数据集

Id   Date        col_b_01  col_b_02 col_b_03 col_b_12 col_a_65
1    02/19/2020  0         1        2        0        4
2    02/10/2020  1         2        0        1        3
1    03/11/2020  2         1        3        1        0
4    10/29/2020  1         0        2        1        0

虽然我可以单独重命名它们,但我喜欢更有效地这样做,但不确定如何。非常感谢任何建议。提前致谢。

names(dat)[3:7]
# [1] "Col_a_1"  "Col_a_2"  "Col_a_3"  "Col_a_12" "Col_a_65"
gsub("_([0-9])$", "_0\1", names(dat)[3:7])
# [1] "Col_a_01" "Col_a_02" "Col_a_03" "Col_a_12" "Col_a_65"
names(dat)[3:7] <- gsub("_([0-9])$", "_0\1", names(dat)[3:7])
dat
#   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

您可以在 rename_with -

中使用 str_replace
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

数据

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))