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!
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!
sprintf()
is a function to do c-like formatting, e.g.
sprintf("%06i",c(1331, 221231, 21311))
# [1] "001331" "221231" "021311"
This is called padding zeros.
The function str_pad()
from the stringr
library works like this:
https://www.rdocumentation.org/packages/stringr/versions/1.4.0/topics/str_pad
df<-data.frame(zip=c(1,22,333,4444,55555))
df$zip <- stringr::str_pad(df$zip, width=5, pad = "0")
[1] "00001" "00022" "00333" "04444" "55555"