assert_all_are_us_telephone_numbers

0th

Percentile

Is the string a valid US telephone number?

Checks that the input contains US/Canadian (NANPA) telephone numbers.

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

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

is_us_telephone_number(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_telephone_number returns TRUE if the input string contains a valid US telephone number. The assert_* functions return nothing but throw an error when the is_* function returns FALSE.

Note

A valid US phone number consists of an optional country code (either +1, 001 or just 1), followed by a 3 digit NPA area code, where the first digit is between 2 and 9, and the second and third digits don't match. Next is a 3 digit exchange (NXX) code, where the first digit is between 2 and 9 and the second and third digits aren't 11. Finally there is a four digit subscriber number (with no restrictions). 7 digit numbers (without the NPA code) are not supported here. Canada, parts of the Caribbean, and some Atlantic and Pacific islands also use the same numbering system.

Aliases
  • assert_all_are_us_telephone_numbers
  • assert_any_are_us_telephone_numbers
  • is_us_telephone_number
Examples
# NOT RUN {
phone_numbers <- c(
  "12345678901",   #country code as 1 
  "+12345678901",  #country code as +1
  "0012345678901", #country code as 001
  "2345678901",    #no country code
  "10345678901",   #NPA can't begin 0 
  "11345678901",   #...or 1
  "12335678901",   #2nd, 3rd digits of NPA can't match
  "12340678901",   #NXX can't begin 0        
  "12341678901",   #...or 1
  "12345118901",   #2nd, 3rd digits of NXX can't be 11
  "1234567",       #NPA must be included               
  "12345678"      #ditto
)
is_us_telephone_number(phone_numbers)
# }
Documentation reproduced from package assertive.data.us, version 0.0-2, License: GPL (>= 3)

Community examples

Looks like there are no examples yet.