I have dataframe with several columns like this with str character
df$A <- c("1, 26, 61", "2, 3", "2, 68", "2", "2, 3", "3, 11")
I want to add 0 before a single-digit only
I have tried these codes but I am not getting desired output like this
output= "01, 26, 61", "02, 03", "02, 68", "02", "02, 03", "03, 11"
df$A <- sprintf("%02s",df$A)
df$A <- str_pad(df$A, width=2, pad=0)
stringr::str_pad(df, 2, side="left", pad="0")
>Solution :
Maybe you can try
> x <- c("1, 26, 61", "2, 3", "2, 68", "2", "2, 3", "3, 11")
> gsub("\\b(\\d)\\b", "0\\1", x)
[1] "01, 26, 61" "02, 03" "02, 68" "02" "02, 03"
[6] "03, 11"