How add 0´s in front of a number to make it 6 digits R

Viewed 142

Ive found solutions for my problem in other code languages, but not in R.

I want to add 0s infront of numbers in a column in a data frame to make it 6 digits.

Eg

1331
221231
21311

I want to become

001331
221231
021311

Thanks in advance!

2 Answers

sprintf() is a function to do c-like formatting, e.g.

sprintf("%06i",c(1331, 221231, 21311))
# [1] "001331" "221231" "021311"
Related