Lognormality > phonenumber


12 July 2015 | | categories: projects, r package, rstats, software | blog archive


New phone... by Tessss photo credit: Tessss

-[ background ]-

phonenumber” is a simple R package that converts English letters to numbers or numbers to English letters as on a telephone keypad.

When I recently posted some of my Turbo Pascal Stuff, I found an incomplete program that was supposed to do this. I was active on BBSes and, though I don’t recall the reason, I wanted a way to determine the possible words spelled by the BBS phone numbers (and/or how to determine what phone numbers correspond to words/phrases). I never got around to finishing the second part (numbers to letters) in Turbo Pascal, though.

I decided to create this functionality in R for three reasons:

  1. to see if I could write the functions
  2. to learn to publish a package to CRAN
  3. to serve as a possible pedagogical example for others as it involves working with lists, splitting strings, and the expand.grid function.

-[ telephone keypad ]-

For purposes of this package, the mapping of numbers to letters on a telephone’s keypad are as follows:

  • Default behavior - if parameter qz is omitted (or has a value other than 0):

    • 2 corresponds to A, B, C
    • 3 corresponds to D, E, F
    • 4 corresponds to G, H, I
    • 5 corresponds to J, K, L
    • 6 corresponds to M, N, O
    • 7 corresponds to P, Q, R, S
    • 8 corresponds to T, U, V
    • 9 corresponds to W, X, Y, Z
    • 0 and 1 have no corresponding letters
  • Alternate behavior - if parameter qz = 0:

    • 2 corresponds to A, B, C
    • 3 corresponds to D, E, F
    • 4 corresponds to G, H, I
    • 5 corresponds to J, K, L
    • 6 corresponds to M, N, O
    • 7 corresponds to P, R, S
    • 8 corresponds to T, U, V
    • 9 corresponds to W, X, Y
    • 0 corresponds to Q, Z
    • 1 has no corresponding letters

-[ installation ]-

  • phonenumber is available on CRAN and can be installed accordingly:
    install.packages("phonenumber")
    library(phonenumber)
  • updates to phonenumber can be installed from GitHub using the devtools package:
    install.packages("devtools")
    library(devtools)
    install_github("scumdogsteev/phonenumber")
    library(phonenumber)

-[ functions ]-

phonenumber consists of two functions (that are not limited to any standard length or format of telephone numbers):

  1. letterToNumber - converts words/phrases in English letters into the corresponding numbers as on a telephone keypad, returning a character vector containing a number. Numbers are returned as is.
  2. numberToLetter - converts numbers into the corresponding combinations of English letters as on a telephone keypad, returning a character vector containing all possible combinations sorted alphabetically. Letters are returned as is.

Both functions convert non-alphanumeric characters to dashes (-) as that is the character that usually separates parts of a phone number.

-[ examples ]-

letterToNumber:

string <- "Texas" 
letterToNumber(string) 
#> [1] "83927"

numberToLetter:

string <- "22" 
numberToLetter(string) 
#> [1] "AA" "AB" "AC" "BA" "BB" "BC" "CA" "CB" "CC"

-[ license ]-

  • MIT License

-[ releases / changelog ]-

For no particular reason, versions of phonenumber will be named after things from the Terminator universe.

-[ download / install r package ]-



Comments: