将 Stata 代码转换为 R 代码 "Convert numbers into words"

Convert Stata code to R code "Convert numbers into words"

我想将此代码从 Stata 转换为 R,以便将数字转换为单词,或者我可以做同样的事情,但使用 R 命令。我对 Stata 编程一窍不通

我真的希望有人能帮助我。 谢谢!

**** Convertidor de Números en Letras
local nums N21 N22 N23
local lets l21 l22 l23
local N: word count `nums'
forvalues n=1/`N'{
    local t: word `n' of  `lets'
    local z: word `n' of `nums'
        
g `t'=string(`z')
replace `t'=subinstr(`t',"0","CERO",.) if `z'==0
replace `t'=string(floor(`z'/10)*10)+" Y "+string(mod(`z',10)) if inrange(`z',30,99)

local letters `" "30" "40" "50" "60" "70" "80" "90" "100" "29" "28" "27" "26" "25" "24" "23" "22" "21" "20" "19" "18" "17" "16" "15" "14" "13" "12" "11" "10" "9" "8" "7" "6" "5" "4" "3" "2" "1" "Y 0" "'
local values `" "TREINTA" "CUARENTA" "CINCUENTA" "SESENTA" "SETENTA" "OCHENTA" "NOVENTA" "CIEN" "VEINTINUEVE" "VEINTIOCHO" "VEINTISIETE" "VEINTISEIS" "VEINTICINCO" "VEINTICUATRO" "VEINTITRES" "VEINTIDOS" "VEINTIUNO" "VEINTE" "DIECINUEVE" "DIECIOCHO" "DIECISIETE" "DIECISEIS" "QUINCE" "CATORCE" "TRECE" "DOCE" "ONCE" "DIEZ" "NUEVE" "OCHO" "SIETE" "SEIS" "CINCO" "CUATRO" "TRES" "DOS" "UNO" "" "'
local N: word count `letters'
forvalues i=1/`N' {
        local l : word `i' of `letters'
        local x: word `i' of `values'
        replace `t'=subinstr(`t',"`l'","`x'",.) if inrange(`z',1,100)
}
    *** 101 a 1,000
replace `t'=string(floor(`z'/100)*100)+" "+ string(mod(floor(`z'/10)*10,100))+" Y "+string(mod(mod(`z',100),10)) if inrange(`z',101,1000)
replace `t'=subinstr(`t'," 0 Y 0"," ",.)
replace `t'=subinstr(`t'," 0 Y "," ",.)
replace `t'=subinstr(`t'," Y 0"," ",.)

local letters `" "1000" "100" "200" "300" "400" "500" "600" "700" "800" "900" "30" "40" "50" "60" "70" "80" "90"  "20 Y 9" "20 Y 8" "20 Y 7" "20 Y 6" "20 Y 5" "20 Y 4" "20 Y 3" "20 Y 2" "20 Y 1" "20" "10 Y 9" "10 Y 8" "10 Y 7" "10 Y 6" "10 Y 5" "10 Y 4" "10 Y 3" "10 Y 2" "10 Y 1" "10" "9" "8" "7" "6" "5" "4" "3" "2" "1" "'
local values `" "MIL" "CIENTO" "DOSCIENTOS" "TRESCIENTOS" "CUATROCIENTOS" "QUINIENTOS" "SEISCIENTOS" "SETECIENTOS" "OCHOCIENTOS" "NOVECIENTOS""TREINTA" "CUARENTA" "CINCUENTA" "SESENTA" "SETENTA" "OCHENTA" "NOVENTA" "VEINTINUEVE" "VEINTIOCHO" "VEINTISIETE" "VEINTISEIS" "VEINTICINCO" "VEINTICUATRO" "VEINTITRES" "VEINTIDOS" "VEINTIUNO" "VEINTE" "DIECINUEVE" "DIECIOCHO" "DIECISIETE" "DIECISEIS" "QUINCE" "CATORCE" "TRECE" "DOCE" "ONCE" "DIEZ" "NUEVE" "OCHO" "SIETE" "SEIS" "CINCO" "CUATRO" "TRES" "DOS" "UNO" "'
local N: word count `letters'
forvalues i=1/`N' {
        local l : word `i' of `letters'
        local x: word `i' of `values'
        replace `t'=subinstr(`t',"`l'","`x'",.) if inrange(`z',101,1000)
}
    ***1,001 a 1,999
replace `t'=substr(string(`z'),1,1)+"000"+" "+substr(string(`z'),2,1)+"00"+" "+substr(string(`z'),3,1)+"0"+" Y "+substr(string(`z'),4,1) if inrange(`z',1001,1999)
replace `t'=subinstr(`t'," 000 "," ",.) if inrange(`z',1001,1999)
replace `t'=subinstr(`t'," 00 Y "," ",.) if inrange(`z',1001,1999)
replace `t'=subinstr(`t'," 00 "," ",.) if inrange(`z',1001,1999)
replace `t'=subinstr(`t'," Y 0"," ",.) if inrange(`z',1001,1999)


local letters `" "1000" "100 0" "100" "200" "300" "400" "500" "600" "700" "800" "900" "30" "40" "50" "60" "70" "80" "90"  "20 Y 9" "20 Y 8" "20 Y 7" "20 Y 6" "20 Y 5" "20 Y 4" "20 Y 3" "20 Y 2" "20 Y 1" "20" "10 Y 9" "10 Y 8" "10 Y 7" "10 Y 6" "10 Y 5" "10 Y 4" "10 Y 3" "10 Y 2" "10 Y 1" "10" "9" "8" "7" "6" "5" "4" "3" "2" "1" "'
local values `" "MIL" "CIEN" "CIENTO" "DOSCIENTOS" "TRESCIENTOS" "CUATROCIENTOS" "QUINIENTOS" "SEISCIENTOS" "SETECIENTOS" "OCHOCIENTOS" "NOVECIENTOS""TREINTA" "CUARENTA" "CINCUENTA" "SESENTA" "SETENTA" "OCHENTA" "NOVENTA" "VEINTINUEVE" "VEINTIOCHO" "VEINTISIETE" "VEINTISEIS" "VEINTICINCO" "VEINTICUATRO" "VEINTITRES" "VEINTIDOS" "VEINTIUNO" "VEINTE" "DIECINUEVE" "DIECIOCHO" "DIECISIETE" "DIECISEIS" "QUINCE" "CATORCE" "TRECE" "DOCE" "ONCE" "DIEZ" "NUEVE" "OCHO" "SIETE" "SEIS" "CINCO" "CUATRO" "TRES" "DOS" "UNO" "'
local N: word count `letters'
forvalues i=1/`N' {
        local l : word `i' of `letters'
        local x: word `i' of `values'
        replace `t'=subinstr(`t',"`l'","`x'",.) if inrange(`z',1001,1999)
}
replace `t'=subinstr(`t'," 0"," ",.) if inrange(`z',1001,1999)
    ***2,000 a 999,999
replace `t'=substr(string(`z'),1,1)+" MIL "+substr(string(`z'),2,1)+"00"+" "+substr(string(`z'),3,1)+"0"+" Y "+substr(string(`z'),4,1) if inrange(`z',2000,9999)
replace `t'=substr(string(`z'),1,1)+"0 Y "+substr(string(`z'),2,1)+" MIL "+substr(string(`z'),3,1)+"00"+" "+substr(string(`z'),4,1)+"0"+" Y "+substr(string(`z'),5,1) if strlen(string(`z'))==5
replace `t'=substr(string(`z'),1,1)+"00 "+substr(string(`z'),2,1)+"0 Y "+substr(string(`z'),3,1)+" MIL "+substr(string(`z'),4,1)+"00"+" "+substr(string(`z'),5,1)+"0"+" Y "+substr(string(`z'),6,1) if strlen(string(`z'))==6

replace `t'=subinstr(`t'," 000 "," ",.) if inrange(`z',2000,999999)
replace `t'=subinstr(`t'," 00 Y "," ",.) if inrange(`z',2000,999999)
replace `t'=subinstr(`t'," 00 "," ",.) if inrange(`z',2000,999999)
replace `t'=subinstr(`t'," Y 0"," ",.) if inrange(`z',2000,999999)

local letters `" "1000" "100 0" "100" "200" "300" "400" "500" "600" "700" "800" "900" "30" "40" "50" "60" "70" "80" "90"  "20 Y 9" "20 Y 8" "20 Y 7" "20 Y 6" "20 Y 5" "20 Y 4" "20 Y 3" "20 Y 2" "20 Y 1" "20" "10 Y 9" "10 Y 8" "10 Y 7" "10 Y 6" "10 Y 5" "10 Y 4" "10 Y 3" "10 Y 2" "10 Y 1" "10" "9" "8" "7" "6" "5" "4" "3" "2" "1" "'
local values `" "MIL" "CIEN" "CIENTO" "DOSCIENTOS" "TRESCIENTOS" "CUATROCIENTOS" "QUINIENTOS" "SEISCIENTOS" "SETECIENTOS" "OCHOCIENTOS" "NOVECIENTOS""TREINTA" "CUARENTA" "CINCUENTA" "SESENTA" "SETENTA" "OCHENTA" "NOVENTA" "VEINTINUEVE" "VEINTIOCHO" "VEINTISIETE" "VEINTISEIS" "VEINTICINCO" "VEINTICUATRO" "VEINTITRES" "VEINTIDOS" "VEINTIUNO" "VEINTE" "DIECINUEVE" "DIECIOCHO" "DIECISIETE" "DIECISEIS" "QUINCE" "CATORCE" "TRECE" "DOCE" "ONCE" "DIEZ" "NUEVE" "OCHO" "SIETE" "SEIS" "CINCO" "CUATRO" "TRES" "DOS" "UNO" "'
local N: word count `letters'
forvalues i=1/`N' {
        local l : word `i' of `letters'
        local x: word `i' of `values'
        replace `t'=subinstr(`t',"`l'","`x'",.) if inrange(`z',2000,999999)
}
replace `t'=subinstr(`t'," 0"," ",.) if inrange(`z',2000,999999)

replace `t'=strtrim(itrim(`t'))
}
replace rz21=subinstr(rz21,string(N21),l21,.)
replace rz22=subinstr(rz22,string(N22),l22,.)
replace rz23=subinstr(rz23,string(N23),l23,.)
drop N21 N22 N23 l21 l22 l23
        ****************************

我尝试使用西班牙语的 R 函数(我需要这种语言的函数)来执行此操作,但它只适用于 1-1000,我需要对大于 1000 的数字执行此操作。或者可能是英语功能,然后是翻译功能。 谢谢!

Dictionary: Stata to R 这样的资源将帮助您入门。

但是,正如 Ben Bolker 所建议的,您实际上只需要一个将数字转换为西班牙文名称的 R 脚本。 spanish 包,available from CRAN is just this. See the documentation 用于 to_numberto_words,特别是。

感谢您的帮助!!

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]], "mil", 
                                      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("", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete",
            "ocho", "nueve") 
  names(ones) <- 0:9 
  teens <- c("diez", "once", "doce", "trece", "catorce", "quince",
             "dieciséis", "dieciséis", "dieciocho", "diecinueve")
  names(teens) <- 0:9 
  tens <- c("veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta",
            "noventa") 
  names(tens) <- 2:9 
  x <- round(x)
  suffixes <- c("mil", "millón", "billón", "trillón")     
  if (length(x) > 1) return(sapply(x, helper))     
  helper(x) 
}