你可以用
str_replace
在里面
rename_with
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))