1

my data set has codes which are supposed to be 5 digits. However, there are a few with 4 digits. I want to add leading zeros in front of those values with 4 digits. I used the following code:

labos$postal1 <- sprintf("%05d", labos$postal)

This command added zeros in front of all the values, including those with 5 digits.

Help me add zeros just to the values with 4 digits.

duckmayr
  • 16,303
  • 3
  • 35
  • 53
alba
  • 11
  • 1
  • 2
    Your command is working for me: `test <- c(1234,12345,12)` `sprintf("%05d", test)` gives me `[1] "01234" "12345" "00012"` – f.lechleitner Mar 07 '19 at 13:38

0 Answers0