how would I be able to go from a column like this: 1 1 1 2 3 4 9 25 100
to one like this: 01 01 01 02 03 04 09 25 100
Basically, I want to add a leading zero to any value with only length 1. The actual data frame is much bigger than this. Thanks!
how would I be able to go from a column like this: 1 1 1 2 3 4 9 25 100
to one like this: 01 01 01 02 03 04 09 25 100
Basically, I want to add a leading zero to any value with only length 1. The actual data frame is much bigger than this. Thanks!
You can use sprintf
in base R
vec <- c(1, 1, 1, 2, 3, 4, 9, 25, 100)
sprintf("%02d", vec)
#> [1] "01" "01" "01" "02" "03" "04" "09" "25" "100"
As an alternative str_pad
from stringr
package:
library(stringr)
str_pad(vec, 2, pad = "0")
[1] "01" "01" "01" "02" "03" "04" "09" "25" "100"