r语言 - 将百万/十亿缩写更改为实际数字?即。5.12M -> 5,120,000



正如标题所示,我正在寻找一种将缩写的"字符"文本转换为数字数据的方法。例如,我想在我的数据帧中进行以下更改:

84.06M -> 84,060,000
30.12B -> 30,120,000,000
9.78B -> 9,780,000,000
251.29M -> 251,29,000

以下是我正在使用的一些数据帧的示例:

Index Market Cap    Income   Sales Book/sh
ZX              -     84.06M    -1.50M 359.50M    7.42
ZTS       S&P 500     30.13B   878.00M   5.02B    3.49
ZTR             -          -         -       -       -
ZTO             -      9.78B   288.30M   1.47B    4.28
ZPIN            -      1.02B    27.40M 285.20M    4.27
ZOES            -    251.29M    -0.20M 294.10M    6.79
ZNH             -     10.92B   757.40M  17.26B   33.23
ZF              -          -         -       -       -
ZEN             -      2.78B  -106.70M 363.60M    3.09
ZBK             -      6.06B         -   2.46B   34.65
ZBH       S&P 500     22.76B   712.00M   7.78B   50.94

有人有什么建议吗?我在想基地的gsub…

你可以试试这个

num <- c("1.23M", "15.69B", "123.999M")
num <- gsub('B', 'e9', num)
num <- gsub('M', 'e6', num)
format(as.numeric(num), scientific = FALSE, big.mark = ",")
"84,060,000" "30,120,000,000" "251,290,000"

试试这个:

income <- c("84.06M", "30.12B", "251.29M")
toInteger <- function(income){
amt <- as.numeric(gsub("[A-Z]", "", income))
multiplier <- substring(income, nchar(income))
multiplier <- dplyr::case_when(multiplier == "M" ~ 1e6,
multiplier == "B" ~ 1e9,
TRUE ~ 1) # you can add on other conditions for more suffixes
amt*multiplier
}
>toInteger(income)
[1] 8.4060e+07 3.0120e+10 2.5129e+08

您可以这样更改所有列:

test = c("30.13B","84.06M","84.06B","84.06M")
values = sapply(strsplit(test,c("B","M")),function(x) as.numeric(x))
amount = sapply(strsplit(test,""), function(x) x[length(x)])
values2 = sapply(1:length(amount),function(x) ifelse(amount[x] == "B",values[x]*1e9,values[x]*1e6))

只需将test替换为要更改的数据帧列,并将value替换为数据帧名称和要更改的列

最新更新