knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(dialr) # load dplyr here to avoid mask warnings library(dplyr, warn.conflicts = FALSE)
dialr is an R interface to Google's libphonenumber java library.
libphonenumber defines the PhoneNumberUtil
class, a set of functions for extracting information from and performing processing on a parsed Phonenumber
object. A phone number must be parsed before any other operations (e.g. checking phone number validity, formatting) can be performed.
dialr provides an interface to these functions to easily parse and process phone numbers in R.
A phone class vector stores a parsed java Phonenumber
object for further processing alongside the original raw text phone number and default region. This "default region" is required to determine the processing context for non-international numbers.
To create a phone vector, use the phone()
function. This takes a character vector of phone numbers to parse and a default region for phone numbers not stored in an international format (i.e. with a leading "+").
library(dialr) # Parse phone number vector x <- c(0, 0123, "0404 753 123", "61410123817", "+12015550123") x <- phone(x, "AU") is.phone(x) print(x)
is_parsed(x) # Was the phone number successfully parsed? is_valid(x) # Is the phone number valid? is_possible(x) # Is the phone number possible? get_region(x) # What region (ISO country code) is the phone number from? get_type(x) # Is the phone number a fixed line, mobile etc.
Equality comparisons for phone numbers ignore formatting differences and compare the underlying phone number.
phone("0404 753 123", "AU") == phone("+61404753123", "US") phone("0404 753 123", "AU") == phone("0404 753 123", "US") phone("0404 753 123", "AU") != phone("0404 753 123", "US")
Parsed phone numbers can also be compared to character phone numbers stored in an international format.
phone("0404 753 123", "AU") == c("+61404753123", "0404 753 123")
Use is_match()
for more customisable comparisons.
is_match(phone("0404 753 123", "AU"), c("+61404753123", "0404753123", "1234")) is_match(phone("0404 753 123", "AU"), c("+61404753123", "0404753123", "1234"), detailed = TRUE) is_match(phone("0404 753 123", "AU"), c("+61404753123", "0404753123", "1234"), strict = FALSE)
The phone class has a format()
method implementing libphonenumber's core formatting functionality.
There are four phone number formats used by libphonenumber (see "Further reading" for details): "E164"
, "NATIONAL"
, "INTERNATIONAL"
and"RFC3966"
.
These can be specified by the format
argument, or a default can be specifed in option dialr.format
.
If clean = TRUE
, all non-numeric characters are removed except for a leading +
. clean = TRUE
by default.
x <- phone(c(0, 0123, "0404 753 123", "61410123817", "+12015550123"), "AU") format(x, format = "RFC3966") format(x, format = "RFC3966", clean = FALSE) format(x, format = "E164", clean = FALSE) format(x, format = "NATIONAL", clean = FALSE) format(x, format = "INTERNATIONAL", clean = FALSE) format(x, format = "RFC3966", clean = FALSE) # Change the default getOption("dialr.format") format(x) options(dialr.format = "NATIONAL") format(x) options(dialr.format = "E164")
If the home
argument is supplied, the phone number is formatted for dialling from the specified country.
format(x, home = "AU") format(x, home = "US") format(x, home = "JP")
If strict = TRUE
, invalid phone numbers (determined using is_valid()
) return NA
.
format(x) format(x, strict = TRUE)
By default, as.character()
returns the raw text phone number. Use raw = FALSE
to use the format()
method instead.
as.character(x) as.character(x, raw = FALSE)
dialr functions are designed to work well in dplyr workflows.
# Use with dplyr library(dplyr) y <- tibble(id = 1:4, phone1 = c(0, 0123, "0404 753 123", "61410123817"), phone2 = c("03 9388 1234", 1234, "+12015550123", 0), country = c("AU", "AU", "AU", "AU")) y %>% mutate_at(vars(matches("^phone")), ~phone(., country)) %>% mutate_at(vars(matches("^phone")), list(valid = is_valid, region = get_region, type = get_type, clean = format))
Falsehoods Programmers Believe About Phone Numbers
"E164"
: general format for international telephone numbers from ITU-T Recommendation E.164
"NATIONAL"
: national notation from ITU-T Recommendation E.123
"INTERNATIONAL"
: international notation from ITU-T Recommendation E.123
"RFC3966"
: "tel" URI syntax from the IETF tel URI for Telephone Numbers
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.