如何在 R 中将整数转换为用单词书写的整数?


如果我们有一些数字,我们可能希望将这些数字转换成单词形式。例如,将 1 转换成一。在文本数据包括数字的情况下,这可能是必需的。因此,最好用文本形式表示数字以实现文本的一致性。这可以使用 qdap 包中的 replace_number 函数来实现。

安装和加载 qdap 包−

install.packages("qdap")
library("qdap")

示例

replace_number("1")
[1] "one"
replace_number("10")
[1] "ten" replace_number("100")
[1] "one hundred"
replace_number("1000")
[1] "one thousand"
replace_number("1001")
[1] "one thousand one"
replace_number("12000")
[1] "twelve thousand"
replace_number("12214")
[1] "twelve thousand two hundred fourteen"
replace_number("15748")
[1] "fifteen thousand seven hundred forty eight" replace_number("2361414")
[1] "two million three hundred sixty one thousand four hundred fourteen" replace_number("321457")
[1] "three hundred twenty one thousand four hundred fifty seven" replace_number(c("1","2","3","4","5","6","7","8","9","10"))
[1] "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"
[10] "ten" replace_number(c("321457","214521","365841"))
[1] "three hundred twenty one thousand four hundred fifty seven"
[2] "two hundred fourteen thousand five hundred twenty one"
[3] "three hundred sixty five thousand eight hundred forty one" replace_number(c("264155","154512","344512","2144","221","14542","35915112","1454221","147232321","14789523"))
[1] "two hundred sixty four thousand one hundred fifty five"
[2] "one hundred fifty four thousand five hundred twelve"
[3] "three hundred forty four thousand five hundred twelve"
[4] "two thousand one hundred forty four"
[5] "two hundred twenty one"
[6] "fourteen thousand five hundred forty two"
[7] "thirty five million nine hundred fifteen thousand one hundred twelve"
[8] "one million four hundred fifty four thousand two hundred twenty one"
[9] "one hundred forty seven million two hundred thirty two thousand three hundred twenty one"
[10] "fourteen million seven hundred eighty nine thousand five hundred twenty three"

x<-c("印度有 29 个邦") replace_number(x)

[1] "There are twenty nine states in india"

y<-c("1025454 人患上新冠肺炎") replace_number(y)

[1] "one million twenty five thousand four hundred fifty four people suffered from covid-nineteen"

z<-c("世界上有 22 个阿拉伯国家") replace_number(z)

[1] "We have twenty two Arab countries in the world"

v1<-sample(0:5000,20) v1

[1] 1845 4438 4664 1803 1131 4254 4880 4267 4764 4168 222 3880 534 1319 4998
[16] 370 2657 78 1056 620 replace_number(v1)
[1] "one thousand eight hundred forty five"
[2] "four thousand four hundred thirty eight"
[3] "four thousand six hundred sixty four"
[4] "one thousand eight hundred three"
[5] "one thousand one hundred thirty one"
[6] "four thousand two hundred fifty four"
[7] "four thousand eight hundred eighty"
[8] "four thousand two hundred sixty seven"
[9] "four thousand seven hundred sixty four"
[10] "four thousand one hundred sixty eight"
[11] "two hundred twenty two"
[12] "three thousand eight hundred eighty"
[13] "five hundred thirty four"
[14] "one thousand three hundred nineteen"
[15] "four thousand nine hundred ninety eight"
[16] "three hundred seventy"
[17] "two thousand six hundred fifty seven"
[18] "seventy eight"
[19] "one thousand fifty six"
[20] "six hundred twenty"

v2<-sample(100001:999999,20)

v2

[1] 359487 994098 458703 474531 106581 905167 373805 288612 614544 591035
[11] 632526 649772 711979 785355 720779 678609 843081 679009 593148 801554

replace_number(v2)

[1] "three hundred fifty nine thousand four hundred eighty seven"
[2] "nine hundred ninety four thousand ninety eight"
[3] "four hundred fifty eight thousand seven hundred three"
[4] "four hundred seventy four thousand five hundred thirty one"
[5] "one hundred six thousand five hundred eighty one"
[6] "nine hundred five thousand one hundred sixty seven"
[7] "three hundred seventy three thousand eight hundred five"
[8] "two hundred eighty eight thousand six hundred twelve"
[9] "six hundred fourteen thousand five hundred forty four"
[10] "five hundred ninety one thousand thirty five"
[11] "six hundred thirty two thousand five hundred twenty six"
[12] "six hundred forty nine thousand seven hundred seventy two"
[13] "seven hundred eleven thousand nine hundred seventy nine"
[14] "seven hundred eighty five thousand three hundred fifty five"
[15] "seven hundred twenty thousand seven hundred seventy nine"
[16] "six hundred seventy eight thousand six hundred nine"
[17] "eight hundred forty three thousand eighty one"
[18] "six hundred seventy nine thousand nine"
[19] "five hundred ninety three thousand one hundred forty eight"
[20] "eight hundred one thousand five hundred fifty four"

更新于: 08-10-2020

66 次浏览

开启您的 职业生涯

通过完成本课程获得认证

开始
广告