0

in my dataframe im gathering all the powerball numbers and dollar amounts together based on the dates it was drawn. I need to convert my JackpotAmount column from giving me results as "90.00 million" to actual numbers and make it numeric data. I have 1800 rows, so individual changes havent worked and im a little stuck. I tried using gsub to 1e6, but it ended up reading out as things like 90.00 e6 with the space inbetween. Thanks!

Sebastien
  • 9
  • 1
  • does this not work? gsub("\\.00 million", "000000", "90.00 million") – Khaynes Nov 23 '19 at 21:12
  • This post might help you https://stackoverflow.com/questions/45972571/changing-million-billion-abbreviations-into-actual-numbers-ie-5-12m-5-120-0 – Ronak Shah Nov 24 '19 at 11:21

1 Answers1

0

in the future you might want to supply with some example data showcasing your particular problem. Assuming that your colum is only strings like "90 million" and "1 million", you can simply replace the "million" part with corresponding number of zeroes. This can be accomplished with gsub or something like str_replace() from the tidyverse and then convert to integer. It could look like this:

df %>%
  mutate_at("jackpot_amount", ~as.numeric(str_replace(.x, "\\smillion", "000000")))

Which should solve your problem.

Peter H.
  • 1,995
  • 8
  • 26
  • This may succeed if there are no other word-amounts and no "$" but would result in erroneous NA's if there were any such or any commas. Should also have a library call for the package(s) from which you expect that function(s) to be obtained. – IRTFM Nov 23 '19 at 22:26
  • Yes, but I wrote "assuming that the column is only strings like..." – Peter H. Nov 25 '19 at 12:52