Learn R Programming

assertive (version 0.1-7)

assert_all_are_us_zip_codes: Is the string a valid US zip code?

Description

Checks that the input contains US zip codes.

Usage

assert_all_are_us_zip_codes(x)

  assert_any_are_us_zip_codes(x)

  is_us_zip_code(x)

Arguments

x
Input to check.

Value

  • is_us_zip_code returns TRUE if the input string contains a valid US zip code. The {assert_*} functions return nothing but throw an error when the is_* function returns FALSE.

References

Regexes inferred from https://en.wikipedia.org/wiki/ZIP_code and https://en.wikipedia.org/wiki/List_of_ZIP_code_prefixes.

Examples

Run this code
zip_codes <- c(
  "90210",
  "20500",
  "22313-1450",  #5+4 style ok
  "223131450",   #fails, no hyphen
  "09901"        #fails, invalid area prefix
 )
is_us_zip_code(zip_codes)
assert_any_are_us_zip_codes(zip_codes)
#The following code should throw an error.
assert_all_are_us_zip_codes(zip_codes)

Run the code above in your browser using DataLab