Convert from billion to million and vice versa

后端 未结 6 539
旧巷少年郎
旧巷少年郎 2020-11-30 15:27

Suppose I have the following data frame named DF. I would like to convert all the values in the Revenue column to the same unit.

         


        
6条回答
  •  既然无缘
    2020-11-30 15:33

    We can also do this with gsubfn. Replace the 'bn', 'Mn' with * 1, * 1/1000, evaluate the string and paste with 'bn'.

    library(gsubfn)
    sprintf("%.2f bn", sapply(gsubfn("([[:alpha:]]+)", list(Mn = "* 1/1000", 
            bn = "* 1"), df1$Revenue), function(x) eval(parse(text=x))))
    #[1] "50.10 bn" "41.20 bn" "0.03 bn"  "15.10 bn"
    

提交回复
热议问题