There has to be a simple way of doing this and I am overlooking it. But if I have a series of id
and want to add trailing zeros where the character limit is not
If we insist - we can fool sprintf()
into doing what it doesn't want to do:
# Helper function from: stackoverflow.com/a/13613183/4552295
strReverse <- function(x) sapply(lapply(strsplit(x, NULL), rev), paste, collapse="")
so_0padr <- function(x, width = 5) {
strReverse(
sprintf(
"%0*d",
width,
as.integer(
strReverse(
as.character(x)
)
)
)
)
}
Resulting in
so_0padr(c(2331,29623,311,29623))
[1] "23310" "29623" "31100" "29623"