将“百万/十亿”缩写转换为实际数字?例如:5.12M -> 5,120,000。

3

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

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

有人有什么建议吗?我在想使用基础R中的gsub...


如果这些数据来自另一种格式,可能会有更直接的方法。例如,如果它们是 Excel 工作簿中的值,则最好使用 readxl::read_excel(the-excel-file.xlsx) - Hugh
3个回答

8
你可以尝试这个。
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"

1

试试这个:

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

1
你可以像这样更改所有的列:

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))

只需将测试替换为您要更改的数据框列,value为数据框名称和您要更改的列名


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接