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