0

I have a column in a dataset that looks like this :

  • Actual
  • chr
  • 5.25%
  • -5.50*1000000000
  • 0.24%
  • -4.00*1000
  • 4.5%

My goal is to access it and automatically convert the cells that have *1000 or *1000000000 and make the calculation, ex -5.5 * 1000000000 should be - 5 500 000 000 on the cell and -4 * 1000 should be -4000.

Does anyone have a hint how to do this?

Best regards

Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34

2 Answers2

1

If your string is guaranteed to be a valid expression that R can evaluate literally, you can use

eval(parse(text = '-5*1000'))

This parses the string into R code equivalent, and then executes it using eval. In this case, it leads to a numerical result of -5000.

Tread with care. More background on using eval(parse) can be found here

1

This can be done using first a splitting operation on * and then a mapping operation based on purrr's function map_dbl to perform the calculations:

library(purrr)                                   
library(dplyr)
df %>%
  # Step 1: split strings on `*`:
  mutate(x_new = strsplit(x,"\\*")) %>%
  # Step 2: convert to numeric and perform calculation:
  mutate(x_new = ifelse(str_detect(x_new, ","), 
                        map_dbl(x_new, function(x) as.numeric(x)[1] * as.numeric(x)[2]),
                        x_new))
                 x    x_new
1 -5.50*1000000000 -5.5e+09
2              35%      35%
3       -4.00*1000    -4000

(warning messages can be ignored)

Test data:

df <- data.frame(x = c("-5.50*1000000000", "35%", "-4.00*1000"))
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34