I have the following data
GT-BU7867-09
GT-BU6523-113
GT-BU6452-1
GT-BU8921-12
How do I use R to make the numbers after the hyphen to pad leading zeros so it will have three digits? The resulting format should look like this:
GT-BU7867-009
GT-BU6523-113
GT-BU6452-001
GT-BU8921-012
Base solution:
sapply(strsplit(x,"-"), function(x)
paste(x[1], x[2], sprintf("%03d",as.numeric(x[3])), sep="-")
)
Result:
[1] "GT-BU7867-009" "GT-BU6523-113" "GT-BU6452-001" "GT-BU8921-012"
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