0

I have a homework to analyze data of Bloomberg Billionaires Index in R Studio, but I am facing a problem with the periods.. There are three forms of values:

  1. 185B (No periods)
  2. 18.5B (one digit after the period)
  3. 1.85B (two digits after the period)

I want to delete the dots and add nine zeros in place of the billion symbol (B) but that means the three values will be the same. Is there a way to add:

  • Nine zeros for the first formula (where there are no points)
  • Eight zeros for the second formula (where there is one digit after the period)
  • Seven zeros for the third formula (where there are two digits after the period)

Thank you!!

  • How about removing the B and multiplying with 10^9? – Martin Gal Jun 08 '21 at 13:31
  • It worked! Thanks a lot! in the next column, there are B, M and K. Is there a way to apply a different multiply function for each of them? I mean the "$ Last change" column in this set: https://www.bloomberg.com/billionaires/ Thank you! – Muhannad Hammoud Jun 08 '21 at 14:29
  • Thanks! Finally I solved it with ifelse: df$v = ifelse(grepl("B", df$x) ,df$y*10^9, ifelse(grepl("M", df$x), df$y*10^6, df$y*10^3)) – Muhannad Hammoud Jun 08 '21 at 18:21

2 Answers2

0
x <- c('185B', '18.5B', '1.85B')
as.numeric(sub('B', '', x, fixed = TRUE)) * 10^9

If use of packages are allowed you can use readr::parse_number to get the number directly.

readr::parse_number(x) * 10^9
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you! But there are 500 values is the column (185 is just an example). What should I write to include them all? – Muhannad Hammoud Jun 08 '21 at 14:30
  • Instead of `x` you have to pass the column value. If your dataframe is called `df` and column is called `col` you can replace `x` with `df$col`. – Ronak Shah Jun 08 '21 at 14:31
  • I got it. Thanks! What if the column contains B, M and K. Is there a way to apply different multiply function for each of them. I mean the "$ Last change" column in this set: https://www.bloomberg.com/billionaires/ Thank you! – Muhannad Hammoud Jun 08 '21 at 14:46
  • You can use `ifelse` to do that. If you have trouble implementing ask that as a new question. – Ronak Shah Jun 08 '21 at 14:58
  • Thanks a lot! I finally solved it with ifelse: : df$v = ifelse(grepl("B", df$x) ,df$y*10^9, ifelse(grepl("M", df$x), df$y*10^6, df$y*10^3)) – Muhannad Hammoud Jun 08 '21 at 18:31
0

We can use

library(stringr)
as.numeric(str_remove(x, 'B')) * 10 ^9
#[1] 1.85e+11 1.85e+10 1.85e+09

data

x <- c('185B', '18.5B', '1.85B')
akrun
  • 874,273
  • 37
  • 540
  • 662