I am aware of all of the questions regarding the Adding leading zero and the comprehensive responses provided for them such as Q1, Q2, Q3.
But to me , at least based on my current knowledge, I am not able to address what I am going to do as follow:
leading zero in a string using regex pattern match
So, I want to add leading zero only to digits after the -.
for example :
Sam <- c("222-88", "537-457", "652-1", "787-892")
var <- LETTERS[1:4]
DF<- data.frame(Sam, var)
DF
Sam var
1 222-88 A
2 537-457 B
3 652-1 C
4 787-892 D
Expected results:
Sam var
1 222-088 A
2 537-457 B
3 652-001 C
4 787-892 D
I tried :
library(stringr)
temp <- DF[str_detect(DF$Sam, "-[0-9]{1,2}$"),] # will find the rows need the leading zero
temp
Sam var
1 222-88 A
3 652-1 C
formatC(temp$Sam, width = 2,flag = 0)# not correct!
We can do this with base R, by splitting the string by - and then use sprintf to pad the 0's after converting to numeric and then paste
DF$Sam <- sapply(strsplit(as.character(DF$Sam), "-"), function(x)
paste(x[1],sprintf("%03d", as.numeric(x[2])), sep="-"))
DF$Sam
#[1] "222-088" "537-457" "652-001" "787-892"
If we need a regex approach we can use gsubfn
library(gsubfn)
gsubfn("(\\d+)$", ~sprintf("%03d", as.numeric(x)), as.character(DF$Sam))
#[1] "222-088" "537-457" "652-001" "787-892"
Another base option
Sam <- c("222-88", "537-457", "652-1", "787-892")
m <- gregexpr("[0-9]+$", Sam)
regmatches(Sam, m) <- sprintf('%03s', unlist(regmatches(Sam, m)))
Sam
# [1] "222-088" "537-457" "652-001" "787-892"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With