将百万/十亿缩写改为实际数字?即。 5.12M - > 5120000

时间:2017-08-31 03:05:35

标签: r dataframe tidyr

正如标题所暗示的那样,我正在寻找一种方法来改造短手缩短的角色'文本到数字数据。例如,我想在我的数据框中进行这些更改:

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

3 个答案:

答案 0 :(得分:6)

你可以试试这个

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 :(得分: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

答案 2 :(得分: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))

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