2017-07-06 91 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 

希望這有助於!