This function works best when converting numbers to letters, as each number only has a single possible letter. For each letter, there are 3 or 4 possible letters, resulting in a number of possible conversions. This function was intended to convert phonetic telephone numbers to their valid numeric equivalent; when used in this manner, each letter in a string can be lazily replaced without changing the rest of the string.

keypad_convert(x, ext = FALSE)

Arguments

x

A vector of characters or letters.

ext

logical; Should extension text be converted to numbers. Defaults to FALSE and matches x, ext, and extension followed by a space or number.

Value

If a character vector is supplied, a vector of each elements numeric counterpart is returned. If a numeric vector (or a completely coercible character vector) is supplied, then a list is returned, each element of which contacts a vector of letters for each number.

Details

When replacing letters, this function relies on the feature of stringr::str_replace_all() to work with named vectors (c("A" = "2")).

Examples

keypad_convert("1-800-CASH-NOW ext123")
#> [1] "1-800-2274-669 ext123"
keypad_convert(c("abc", "123"))
#> [1] "222" "123"
keypad_convert(letters)
#>  [1] "2" "2" "2" "3" "3" "3" "4" "4" "4" "5" "5" "5" "6" "6" "6" "7" "7" "7" "7"
#> [20] "8" "8" "8" "9" "9" "9" "9"