Return consistent version US ZIP codes using stringr::str_*() functions. Non-number characters are removed, strings are padded with zeroes on the left, and ZIP+4 suffixes are removed. Invalid ZIP codes from a vector can be removed as well as single (repeating) character strings.

normal_zip(zip, na = c("", "NA"), na_rep = FALSE, pad = FALSE)

Arguments

zip

A vector of US ZIP codes.

na

A vector of values to pass to na_in().

na_rep

logical; If TRUE, na_rep() will be called. Please note that 22222, 44444, and 55555 valid ZIP codes that will not be removed.

pad

logical; Should ZIP codes less than five digits be padded with a leading zero? Leading zeros (as are found in New England ZIP codes) are often dropped by programs like Microsoft Excel when parsed as numeric values.

Value

A character vector of normalized 5-digit ZIP codes.

See also

Other geographic normalization functions: abbrev_full(), abbrev_state(), check_city(), expand_abbrev(), expand_state(), fetch_city(), normal_address(), normal_city(), normal_state(), str_normal()

Examples

normal_zip(
  zip = c("05672-5563", "N/A", "05401", "5819", "00000"),
  na = c("", "NA"),
  na_rep = TRUE,
  pad = TRUE
)
#> [1] "05672" NA      "05401" "05819" NA