I'm looking for an R packet that converts a number into the number written in length. The functionality would be, for example, "134" = "cento e trinta e quatro"
.
Does anyone know?
I'm looking for an R packet that converts a number into the number written in length. The functionality would be, for example, "134" = "cento e trinta e quatro"
.
Does anyone know?
As far as I know, nothing is ready, but because the rules are simple it's not hard to program.
You need some de-paras:
excessoes <- data.frame(
num = 11:19,
nome = c("onze", "doze", "treze", "catorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove"),
stringsAsFactors = F
)
unidades <- data.frame(
num = 1:9,
nome = c("um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove"),
stringsAsFactors = F
)
dezenas <- data.frame(
num = 1:9,
nome = c("dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta", "setenta", "oitenta", "noventa"),
stringsAsFactors = F
)
Next to a function that joins according to the rules of numerals.
library(stringr)
escrever_numero <- function(x){
tamanho <- str_length(x)
num_vetor <- unlist(str_split(x, ""))
if(x %in% excessoes$num){
return(excessoes$nome[excessoes$num == x])
} else {
unidade <- num_vetor[tamanho]
unidade <- unidades$nome[unidades$num == unidade]
if(tamanho > 1){
dezena <- num_vetor[tamanho -1]
dezena <- dezenas$nome[dezenas$num == dezena]
}
}
if(length(unidade) == 0){
return(dezena)
} else if (tamanho > 1){
return(paste(dezena, "e", unidade))
} else{
paste(unidade)
}
}
I did a quick function, which works for numbers from 1 to 99. But if you understand logic it is easy to expand to more numbers.
> escrever_numero(81)
[1] "oitenta e um"
> escrever_numero(99)
[1] "noventa e nove"
> escrever_numero(1)
[1] "um"
> escrever_numero(10)
[1] "dez"
> escrever_numero(15)
[1] "quinze"
It's not very elegant, but it can help ...