당신이 정말로 자신의 알파 등가물에 숫자를 변환하고 싶었 여부를 정확히 밝혀지지 않았다. 그렇다면 훨씬 일반적인 전략이 여기에 있습니다. rhelp 아카이브에는 두 가지 숫자 - 텍스트 변환 함수가 있습니다 (Jim Lemon의 digits2text
및 John Fox의 numberstowords
). 상자 밖으로 일
절단 및 Lemon's function from the HTML found here를 붙여 : 나는 또한 벡터화 된 접근 방식에 도착 gregexpr
로 전환 일부 누락 된 줄 바꿈이 있었기 때문에
> m <- gregexpr("[0-9]+", x)
> sym <- regmatches(x,m)
> regmatches(x,m) <- digits2text(as.numeric(sym[[1]]))
illion = 0
digilen = 3
digitext = three hundred forty six
[1] 6 4 3
>
> x
[1] "I like three hundred forty six ice cream cones. They're three hundred forty six percent good! I ate three hundred forty six."
내가 numberstowords의 일부를 편집 할 필요가 구문 분석을 엉망 (나는이 데모 아래의 성공적인 버전을 포함한다 : http://tolstoy.newcastle.edu.au/R/help/05/04/2715.html
,369 :에서 편집
> m <- gregexpr("[0-9]+", x)
> sym <- regmatches(x,m)
> regmatches(x,m) <- numbers2words(as.numeric(sym[[1]]))
>
> x
[1] "I like three hundred forty six ice cream cones. They're three hundred forty six percent good! I ate three hundred forty six."
폭스의 기능을
numbers2words <- function(x){
helper <- function(x){
digits <- rev(strsplit(as.character(x), "")[[1]])
nDigits <- length(digits)
if (nDigits == 1) as.vector(ones[digits])
else if (nDigits == 2)
if (x <= 19) as.vector(teens[digits[1]])
else trim(paste(tens[digits[2]],
Recall(as.numeric(digits[1]))))
else if (nDigits == 3) trim(paste(ones[digits[3]], "hundred",
Recall(makeNumber(digits[2:1]))))
else {
nSuffix <- ((nDigits + 2) %/% 3) - 1
if (nSuffix > length(suffixes)) stop(paste(x, "is too large!"))
trim(paste(Recall(makeNumber(digits[
nDigits:(3*nSuffix + 1)])),
suffixes[nSuffix],
Recall(makeNumber(digits[(3*nSuffix):1]))))
}
}
trim <- function(text){
gsub("^\ ", "", gsub("\ *$", "", text))
}
makeNumber <- function(...) as.numeric(paste(..., collapse=""))
opts <- options(scipen=100)
on.exit(options(opts))
ones <- c("", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine")
names(ones) <- 0:9
teens <- c("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", " seventeen", "eighteen", "nineteen")
names(teens) <- 0:9
tens <- c("twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety")
names(tens) <- 2:9
x <- round(x)
suffixes <- c("thousand", "million", "billion", "trillion")
if (length(x) > 1) return(sapply(x, helper))
helper(x)
}
직접
numbers2words
기능을 사용하는 완벽한 솔루션은 ...입니다. 방금 게시물을 편집했습니다. 희망을 품지 마라 :) –그 조쉬에 대해 유감스럽게 생각한다. 그것을 돌보고 귀하의 회신에 감사드립니다. 나는 gsubfn 패키지에 대해 몰랐다. 지적 해 주셔서 감사합니다. –