is_us_zip_code: Is the string a valid US zip code?

Description Usage Arguments Value Note References Examples

View source: R/is-data-us.R

Description

Checks that the input contains US zip codes.

Usage

1
2
3
4
5
6
7
assert_all_are_us_zip_codes(x, na_ignore = FALSE,
  severity = getOption("assertive.severity", "stop"))

assert_any_are_us_zip_codes(x, na_ignore = FALSE,
  severity = getOption("assertive.severity", "stop"))

is_us_zip_code(x)

Arguments

x

Input to check.

na_ignore

A logical value. If FALSE, NA values cause an error; otherwise they do not. Like na.rm in many stats package functions, except that the position of the failing values does not change.

severity

How severe should the consequences of the assertion be? Either "stop", "warning", "message", or "none".

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.

Note

A valid zip code is considered to be 5 digits, or 5 digits then a hyphen then 4 digits. Unused area prefixes return FALSE, but the function doesn't guarantee that the zip code actually exists. It should correctly return TRUE for genuine zip codes, and will weed out most badly formatted strings non-existent areas, but some non-existent codes may incorrectly return TRUE. If you need 100 up-to-date zip code base.

References

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
zip_codes <- c(
  "Beverley Hills"  = "90210", 
  "The White House" = "20500", 
  USPTO             = "22313-1450",  #5+4 style ok
  "No hyphen"       = "223131450",
  "Bad area prefix" = "09901",    
  Missing           = NA
 )
is_us_zip_code(zip_codes)
assert_any_are_us_zip_codes(zip_codes)
#The following code should throw an error.
assertive.base::dont_stop(assert_all_are_us_zip_codes(zip_codes))

Example output

There were 3 failures:
  Position     Value      Cause
1        4 223131450 bad format
2        5     09901 bad format
3        6      <NA>    missing
$`assert_all_are_us_zip_codes(zip_codes)`
<assertionError: is_us_zip_code : zip_codes are not all US zip codes.
There were 3 failures:
  Position     Value      Cause
1        4 223131450 bad format
2        5     09901 bad format
3        6      <NA>    missing>

assertive.data.us documentation built on May 2, 2019, 12:22 p.m.