For help with your specific code, can you please turn it into a self-contained reproducible example?
In the meantime, if I'm understanding your goal properly, I think you can do it in one step with stringr::str_pad()
:
library("stringr")
x <- c(99, 999, 9999, 99999)
str_pad(x, width = 5, pad = "0")
#> [1] "00099" "00999" "09999" "99999"
Created on 2018-11-25 by the reprex package (v0.2.1)