0
votes

I would like to concatenate my vector n with 2 characters "0" and "m" .

n=c(18,8,13,24,76,81,96,95)

I tried :

paste0("m", gsub(" ", "0", format(n)))
 [1] "m18" "m08" "m13" "m24" "m76" "m81" "m96" "m95"

The expected result :

[1] "m018" "m008" "m013" "m024" "m076" "m081" "m096" "m095"
1

1 Answers

2
votes

You can use sprintf:

n=c(18,8,13,24,76,81,96,95)
paste0("m" , sprintf("%03d",n))
# "m018" "m008" "m013" "m024" "m076" "m081" "m096" "m095"

Or simply

sprintf("m%03d",n)