Kevin Schaul

Visual journalist/hacker

Fix FIPS codes in R with str_pad()

April 20, 2024

FIPS codes for U.S. counties need leading zeros, but often your data doesn’t have them. Someone down the line may have read them in as numbers, which removed the leading zeroes.

stringr makes fixing that a breeze:

data %>%
  mutate(
    fips = str_pad(fips, 5, pad="0")
  )

h/t Luis Melgar