0

I have a large dataset where I need my industry code to be 5 digits. However, I know that some are 4 digits and so I would like to insert a 0 infront of the value such that it would appear like this: 01234, instead of 1234 (as it is now). Below I show data that I have, and then how I wish the outcome to look like.

What I have:

companyID   year   industry_code
    1       2008       12345
    1       2009       12345
    1       2010       12345
    2       2015        1234
    2       2016        1234
    3       2000        5678
    3       2001        5678
    3       2002        5678
    3       2003        5678

I do not know which ones are 4 digits I just know that they exist.

Needed outcome:

companyID   year   industry_code
    1       2008       12345
    1       2009       12345
    1       2010       12345
    2       2015       01234
    2       2016       01234
    3       2000       05678
    3       2001       05678
    3       2002       05678
    3       2003       05678

thank you in advance, super appreciative of your help!!

2 Answers2

2

Use sprintf . No packages are used.

x <- c(12345, 12345, 1234, 1234, 5678, 5678, 5678, 5678)

sprintf("%05d", x)
## [1] "12345" "12345" "01234" "01234" "05678" "05678" "05678" "05678"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
1
library(stringr)

str_pad(df$industry_code, 5, side = "left", pad = "0")

[1] "12345" "12345" "12345" "01234" "01234" "05678" "05678" "05678" "05678"

Note: to add a zero pad the output will have to be of type character.

LMc
  • 12,577
  • 3
  • 31
  • 43