1

I have a dataset that contains the number of app installers, the data recorded with units M and K, like: 19M, 199K.

How to replace the prefixes with their values to convert values to numeric.

k to e+3

M to e+6

Shir
  • 1,157
  • 13
  • 35
sara bokeh
  • 13
  • 2
  • strsplit(), type.convert(), and switch() will be your friends – MichaelChirico Mar 01 '20 at 03:56
  • Check https://stackoverflow.com/questions/45972571/changing-million-billion-abbreviations-into-actual-numbers-ie-5-12m-5-120-0 , https://stackoverflow.com/questions/38013217/convert-from-billion-to-million-and-vice-versa and https://stackoverflow.com/questions/36806215/convert-from-k-to-thousand-1000-in-r – Ronak Shah Mar 01 '20 at 03:59

1 Answers1

2

Edit: For values that have non-integer values.

x <- c("19M","20K","1K", "1.25M", "1.5K"); x
x <- sub("M", "e6", x); x
x <- sub("K", "e3", x); x
as.numeric(x)
[1] 19000000    20000     1000  1250000     1500

For integer values, the following is sufficient.

x <- c("19M","20K","1K")

x <- sub("M","000000", x)
x <- sub("K","000", x)
as.numeric(x)

1.9e+07 2.0e+04 1.0e+03
Edward
  • 10,360
  • 2
  • 11
  • 26