Sorry, but I'm struggling with a quite simple problem: A formula/an expression like 1.28*10^2 is saved as a character in a data frame. Now I want to convert this string into a numeric value - this should result in "128", but it does not.
library(dplyr)
mydata <- data.frame(
formula = c("5.89*10^3", "1.28*10^2", "4.11*10^5")
)
mydata <- mydata %>%
dplyr::mutate(eval = eval(parse(text = formula)))
The variable "eval" should contain the values 5890, 128 and 411000 in the end.
Can anyone help where my mistake is? Thank you!
Yet another solution, based on rlang::parse_expr
:
library(dplyr)
library(rlang)
mydata <- data.frame(
formula = c("5.89*10^3", "1.28*10^2", "4.11*10^5")
)
mydata %>%
rowwise %>%
mutate(eval = eval(parse_expr(formula)))
#> # A tibble: 3 × 2
#> # Rowwise:
#> formula eval
#> <chr> <dbl>
#> 1 5.89*10^3 5890
#> 2 1.28*10^2 128
#> 3 4.11*10^5 411000