From 16faf22b9feab26f1d27e6152298e06a4b284b7d Mon Sep 17 00:00:00 2001 From: Taha Ahmed Date: Tue, 27 Dec 2011 11:21:31 +0100 Subject: [PATCH] numbers2words() converts numbers into their spelled names. capitalize is... ... a simple function for capitalizing the first letter of a string. It should work well in conjunction with numbers2words() at the beginning of sentences. --- capitalize.R | 17 +++++++++++ numbers2words.R | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 capitalize.R create mode 100644 numbers2words.R diff --git a/capitalize.R b/capitalize.R new file mode 100644 index 0000000..431b3d8 --- /dev/null +++ b/capitalize.R @@ -0,0 +1,17 @@ +################################################## +################## capitalize #################### +################################################## +capitalize <- function(x) { + ## Description: + ## Capitalizes the first letter of a string + ## == This function was inspired by the function supplied in the base R doc for chartr() + ## Usage: + ## capitalize(string) + ## Arguments: + ## x: a string or vector of strings + ## Value: + ## A string or vector of strings + # + paste(toupper(substring(x, 1, 1)), substring(x, 2), + sep = "") +} diff --git a/numbers2words.R b/numbers2words.R new file mode 100644 index 0000000..8c3b438 --- /dev/null +++ b/numbers2words.R @@ -0,0 +1,75 @@ +################################################## +################ numbers2words ################### +################################################## +numbers2words <- function(x, billion = c("US", "UK"), and = if (billion == "US") "" else "and") { + ## Description: + ## Converts a number into its corresponding words in English + ## == THIS FUNCTION WAS PUBLISHED IN: R-News, vol 5, iss 1, May 2005, pp. 51 + ## == Original author: John Fox + ## == Department of Sociology + ## == McMaster University + ## == Hamilton, Ontario + ## == Canada L8S 4M4 + ## == 905-525-9140x23604 + ## == http://socserv.mcmaster.ca/jfox + ## == http://cran.csiro.au/doc/Rnews/Rnews_2005-1.pdf + ## == http://finzi.psych.upenn.edu/R/Rhelp02a/archive/46843.html + ## Usage: + ## numbers2words(number, billion-word, and-or-not) + ## Arguments: + ## x: number + ## billion: follow either US or UK usage rules + ## and: follows the choice set in billion arg + ## Value: + ## Dataframe with the following columns: + ## $ sampleid : chr + # + billion <- match.arg(billion) + trim <- function(text) { + gsub("(^\ *)|((\ *|-|,\ zero|-zero)$)", "", text) + } + makeNumber <- function(x) as.numeric(paste(x, collapse = "")) + makeDigits <- function(x) strsplit(as.character(x), "")[[1]] + helper <- function(x) { + negative <- x < 0 + x <- abs(x) + digits <- makeDigits(x) + nDigits <- length(digits) + result <- if (nDigits == 1) as.vector(ones[digits]) + else if (nDigits == 2) + if (x <= 19) as.vector(teens[digits[2]]) + else trim(paste(tens[digits[1]], "-", ones[digits[2]], sep="")) + else if (nDigits == 3) { + tail <- makeNumber(digits[2:3]) + if (tail == 0) paste(ones[digits[1]], "hundred") + else trim(paste(ones[digits[1]], trim(paste("hundred", and)), + helper(tail))) + } else { + nSuffix <- ((nDigits + 2) %/% 3) - 1 + if (nSuffix > length(suffixes) || nDigits > 15) + stop(paste(x, "is too large!")) + pick <- 1:(nDigits - 3 * nSuffix) + trim(paste(helper(makeNumber(digits[pick])), suffixes[nSuffix], helper(makeNumber(digits[-pick])))) + } + if (billion == "UK") { + words <- strsplit(result, " ")[[1]] + if (length(grep("million,", words)) > 1) + result <- sub(" million, ", ", ", result) + } + if (negative) paste("minus", result) else result + } + opts <- options(scipen = 100) + on.exit(options(opts)) + ones <- c("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine") + teens <- c("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", " seventeen", "eighteen", "nineteen") + names(ones) <- names(teens) <- 0:9 + tens <- c("twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety") + names(tens) <- 2:9 + suffixes <- if (billion == "US") { + c("thousand,", "million,", "billion,", "trillion,") + } else { + c("thousand,", "million,", "thousand million,", "billion,") + } + x <- round(x) + if (length(x) > 1) sapply(x, helper) else helper(x) +}