Search code examples
rstringdplyrmultiple-columnsrename

r rename column names to include 0s


This is my dataset

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

I like to add 0s to those column names that are ending in 1,2,3

The final expected dataset is a dataset with these column names

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

Although I can rename them individually, I like to do this more efficiently but not sure how. Any suggestions much appreciated. Thanks in advance.


Solution

  • You may use str_replace in 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
    

    data

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