I have a set of data which looks something like this:
anim <- c(25499,25500,25501,25502,25503,25504)
sex <- c(1,2,2,1,2,1)
wt <- c(0.8,1.2,1.0,2.
Here's a generalizable base R function:
pad_left <- function(x, len = 1 + max(nchar(x)), char = '0'){
unlist(lapply(x, function(x) {
paste0(
paste(rep(char, len - nchar(x)), collapse = ''),
x
)
}))
}
pad_left(1:100)
I like sprintf
but it comes with caveats like:
however the actual implementation will follow the C99 standard and fine details (especially the behaviour under user error) may depend on the platform