Add leading zeros in alphanumeric string

111 views Asked by At

I have a character vector with strings with a similar format (letters, underscore, digits):

v <- c("AB_124", "BAE_1", "LOED_1234", "H_01")

I want to add leading zeroes to have a length of 4 for the numerical part:

c("AB_0124", "BAE_0001", "LOED_1234", "H_0001")

What are ways to do it?

4

There are 4 answers

0
Darren Tsai On BEST ANSWER

A stringr solution that supplies a function (e.g. str_pad) into str_replace() to replace the match.

library(stringr)

str_replace(v, "(\\d+)$", ~ str_pad(.x, 4, pad = 0))

# [1] "AB_0124"   "BAE_0001"  "LOED_1234" "H_0001"
0
Maël On

One way to go about it (but that I find a bit cumbersome), is to split and reunite:

sapply(strsplit(v, "_"), \(x) {x[2] <- sprintf("%04d", as.numeric(x[2])); paste(x, collapse = "_")})
#[1] "AB_0124"   "BAE_0001"  "LOED_1234" "H_0001"   
1
zx8754 On

Using regex:

sprintf("%s_%04d", substr(v, 1, regexpr("_", v) - 1),
        as.numeric(substring(v, regexpr("_", v) + 1)))
# [1] "AB_0124"   "BAE_0001"  "LOED_1234" "H_0001"

Or stringr:

library(stringr)

str_replace(v, "(\\d+)$", ~sprintf("%04d", as.numeric(.x)))
# [1] "AB_0124"   "BAE_0001"  "LOED_1234" "H_0001"

A bit simpler version of your sapply solution:

sapply(strsplit(v, "_"), \(x){ sprintf("%s_%04d", x[1], as.numeric(x[2])) })
# [1] "AB_0124"   "BAE_0001"  "LOED_1234" "H_0001"
0
Merijn van Tilborg On

Small variation, using data.table's tstrsplit function

s <- data.table::tstrsplit(v, "_", type.convert = TRUE)
sprintf("%s_%04d", s[[1]], s[[2]])

# [1] "AB_0124"   "BAE_0001"  "LOED_1234" "H_0001"