dialr-phone: Phone number parsing and formatting

Description Usage Arguments Details libphonenumber reference See Also Examples

Description

A phone vector stores phone numbers parsed with libphonenumber for formatting and further processing.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
phone(x, region)

phone_reparse(x)

is.phone(x)

## S3 method for class 'phone'
print(x, n = 10, ...)

## S3 method for class 'phone'
format(
  x,
  format = c("E164", "NATIONAL", "INTERNATIONAL", "RFC3966"),
  home = NULL,
  clean = TRUE,
  strict = FALSE,
  ...
)

## S3 method for class 'phone'
as.character(x, raw = TRUE, ...)

Arguments

x

A character vector of phone numbers.

region

A character vector of ISO country codes with the default region for each phone number in x. A region vector of length 1 will be recycled to the length of x.

If NA or "", numbers written in international format (with a leading +) will be parsed without a default region.

n

Number of elements to print.

...

Additional arguments for specific methods.

format

Phone number format to use from one of four standards:

See notes from the libphonenumber javadocs for more details.

format defaults to "E164". The default can be set in option dialr.format.

home

ISO country code for home region. If provided, numbers will be formatted for dialing from the home region.

clean

Should non-numeric characters be removed? If TRUE, keeps numbers and leading "+".

strict

Should invalid phone numbers be removed? If TRUE, invalid phone numbers are replaced with NA.

raw

If TRUE, the raw phone number is returned. Otherwise elements are cleaned with format().

Details

libphonenumber defines the PhoneNumberUtil class, with a set of functions for extracting information from and performing processing on a parsed Phonenumber object. A text phone number must be parsed before any other operations (e.g. checking phone number validity, formatting) can be performed. When parsing a phone number a "default region" is required to determine the processing context for non-international numbers.

A phone vector stores the raw phone number, the default region and a java Phonenumber object for each element. The java object is cached so should persist between R sessions. In case of issues, use phone_reparse() to recreate the phone vector from the original phone number and region.

Phone number parsing functions display a progress bar in interactive sessions by default. This can be disabled by setting option dialr.show_progress to FALSE.

libphonenumber reference

phone(): Phone numbers are parsed using PhoneNumberUtil.parseAndKeepRawInput(). A phone vector stores the returned Phonenumber.PhoneNumber object alongside the original raw text and default region for later reference.

format(): PhoneNumberUtil.format() by default, or PhoneNumberUtil.formatOutOfCountryCallingNumber() if home is provided.

See Also

Other phone functions: dialr-example, dialr-match, dialr-region, dialr-type, dialr-valid, dialr

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
  # Create a phone vector
  x <- phone(c(0, 0123, "0412 345 678", "61412987654", "03 9123 4567", "+12015550123"), "AU")
  
  is.phone(x)
  print(x)
  as.character(x)
  format(x)
  format(x, home = "AU")
  
  # Parse international number with no default region
  phone("+61412345678", NA)
  
  # Will fail to parse if number is not in international format
  phone("0412345678", NA)
  
  # A combination can be used
  phone(c("+61412345678", "0412345678"), c(NA, "AU"))

Example output

OpenJDK 64-Bit Server VM warning: Can't detect primordial thread stack location - find_vma failed
[1] TRUE
# Parsed phone numbers: 6 total, 5 successfully parsed
[1] 0            123          0412 345 678 61412987654  03 9123 4567
[6] +12015550123
[1] "0"            "123"          "0412 345 678" "61412987654"  "03 9123 4567"
[6] "+12015550123"
[1] NA             "+61123"       "+61412345678" "+61412987654" "+61391234567"
[6] "+12015550123"
[1] NA                "123"             "0412345678"      "0412987654"     
[5] "0391234567"      "001112015550123"
# Parsed phone numbers: 1 total, 1 successfully parsed
[1] +61412345678
# Parsed phone numbers: 1 total, 0 successfully parsed
[1] 0412345678
# Parsed phone numbers: 2 total, 2 successfully parsed
[1] +61412345678 0412345678  

dialr documentation built on May 24, 2021, 5:07 p.m.