2017-07-06 92 views
0

在我的数据集中,我有变量,其中数字以大写格式写入。 例如:将数字的大写形式转换为R中的整型

18 123 thousand dollars 
19123 millon dollars 

如何,使用R改造这个格式键入整数? 我的意思是,我需要这种格式。

18 123 000 
19 123 000 000 
+0

但18 123 000是18百万123千...你是怎么做出这种逻辑的? – Sotos

+0

是的,你是对的,所以这个数据来自这种格式的经理!因此“千美元”取代“000” –

+0

“毫升美元”一词替换为“000 000” –

回答

1
options(scipen=999)  //it will prevent long numbers conversion in scientific notation 
df= data.frame(amount = c("18 123 thousand dollars","19 123 million dollars")) 
df$amount <- gsub("thousand dollars","000",df$amount) 
df$amount <- gsub("million dollars","000 000",df$amount) 
df$amount <- gsub("billion dollars","000 000 000",df$amount) 
df$amount <- gsub("\\s+","",df$amount) 
df$amount <- as.numeric(df$amount) 
df 

希望这有助于!