I have a data table from REGIS that includes in random columns, numeric amounts that are summarized by "$1.11 M"(as an example) instead of 1,110,000.00 as a number. In these random columns, it could either show as a "M" or "B" or "K" ( for Millions, Billions, or Thousands).
I am trying to figure out a code that will find and remove the "$" and "M", then multiply the numeric number by 1,000,000 (or whatever the the dollar amount should be).
I have tried using lapply
, if_else
, Gsub
... but I can't figure out how to only make these changes to the cells that have the characters "$" and "M" (or "B"," K")
So far i have:
df1$1<-sapply(gsubfn("[A-Z]", list(K = "*1000", M = "*1e6", B = "*1e9"),
sub("$", "",df1$1, fixed = TRUE)), function(x) eval(parse(text = x)))
The problem is that I am looking for a way to make this code cover all of my columns at once.. just not a specific column.
I expect to change (for example) "$1.65 K" to be 1,650.00 or "3.96 M" to 3,960,000.00