0

I have a data which looks like this

X_STATE STATE_NAME
1       Alabama
2       Alaska
3       Arizona
.
.
9       Connecticut
10      Delaware
11      District of Columbia

The U.S state FIPS codes are in the from 01,02,03,...,09,10,11,...72 and not 1,2,3,...,9,10,11

The X_STATE variable is numeric

I am having some trouble in changing the the state codes from 1,2,3,...,9,10,11 to the correct version which is 01,02,03,...,09,10,11,...72.

My query is, how to make this transformation? Do we need to create some function(s)? I have tried multiple ways but no avail. Your help will be much appreciated.

Thank you

user3571389
  • 335
  • 1
  • 5
  • 10
  • 1
    if you are reading in the data with, say, `read.csv`, and the codes are 01, 02, ... in your file, you can just read those columns as character strings and won't need to pad after the fact – rawr Apr 05 '16 at 19:03

1 Answers1

2
x <- 1:20    
sprintf("%02d", x)
#[1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17"
#[18] "18" "19" "20"
Sotos
  • 51,121
  • 6
  • 32
  • 66