R: Specify sprintf string padding character

Viewed 508

I am trying to create 10-character strings by padding any number with less than 10-characters with zeroes. I've been able to do this with a number of characters less than 10 but the below is causing a 10-character string to form with spaces at the start. How does one make the leading characters 0s?

# Current code
foo <- c("0","999G","0123456789","123456789", "S")
bar <- sprintf("%10s",foo)
bar

# Desired:
c("0000000000","000000999G","0123456789", "0123456789", "00000000S)
1 Answers
Related