sql - R: Specify sprintf string padding character -
i trying create 10-character strings padding number less 10-characters zeroes. i've been able number of characters less 10 below causing 10-character string form spaces @ start. how 1 make leading characters 0s?
# current code foo <- c("0","999g","0123456789","123456789", "s") bar <- sprintf("%10s",foo) bar # desired: c("0000000000","000000999g","0123456789", "0123456789", "00000000s)
we need
sprintf("%010d", as.numeric(foo)) #[1] "0000000000" "0000000999" "0123456789" "0123456789"
if have character
elements, then
library(stringr) str_pad(foo, width = 10, pad = "0") #[1] "0000000000" "000000999g" "0123456789" "0123456789" "000000000s"
Comments
Post a Comment