Convert numbers to letters as on a telephone's keypad

Share:

Description

Take a character vector (i.e., a telephone number) and convert it to all all possible letter combinations as on from a telephone's key pad

Usage

1
numberToLetter(value, decreasing = FALSE, qz = 1)

Arguments

value

An input value as a character vector with one element (a string)

decreasing

Whether to sort the results in alphabetical order or not

qz

Whether to assign q and z to zero (qz = 0) or not (any other value)

Value

A character vector of letters and dashes based on value

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Convert a string or a vector of numeric characters

numberToLetter("911") # returns "W11" "X11" "Y11" "Z11"
x <- "911"
numberToLetter(x) # also returns "W11" "X11" "Y11" "Z11"

# Convert a number directly

numberToLetter(911) # also returns "W11" "X11" "Y11" "Z11"

# Convert an alphanumeric string (letters are returned as is and
# non-alphanumeric characters are returned as dashes)

numberToLetter("dial 911!") # returns "DIAL-W11-" "DIAL-X11-" "DIAL-Y11-" "DIAL-Z11-"

# Specifying qz = 0 maps "q" and "z" to 0 instead of 7 and 9

numberToLetter("000") # returns "000"
numberToLetter("000", qz = 0) # returns "QQQ" "QQZ" "QZQ" "QZZ" "ZQQ" "ZQZ" "ZZQ" "ZZZ"