Home > Software engineering >  How to calculate an equation using eval-parse in R?
How to calculate an equation using eval-parse in R?

Time:02-11

How do I calculate the value of an equation using eval parse in R? The value for each component of the equation should be taken from the eponymously named columns within the same row e.g. 'alpha' from column 'alpha' and so on.

Dataframe:

dput(<alpha beta    gamma   delta   epsilon zeta    eta theta   RF  min.speed.kph   max.speed.kph   speed   equation
9.70E-05    0.0026  1.432   0   0   0   0   0   0   10  130 30  ((alpha*speed^2) (beta*speed) gamma (delta*log10(speed)) (epsilon*exp(zeta*speed)) (eta*(speed^theta)))*(1-RF)
7.40E-05    0.013   1.484   0   0   0   0   0   0   10  130 30  ((alpha*speed^2) (beta*speed) gamma (delta*log10(speed)) (epsilon*exp(zeta*speed)) (eta*(speed^theta)))*(1-RF)
0.000266    -0.014  2.427   0   0   0   0   0   0   10  130 30  ((alpha*speed^2) (beta*speed) gamma (delta*log10(speed)) (epsilon*exp(zeta*speed)) (eta*(speed^theta)))*(1-RF)
2.88E-12    2.56E-10    -2.09E-07   7.75E-05    -0.005274381    0.152658622 0   0.88    NA  10  140 30  ((alpha*speed^5) beta*speed^4 gamma*speed^3 delta*speed^2 epsilon*speed zeta)*(theta)
2.88E-12    2.56E-10    -2.09E-07   7.75E-05    -0.005274381    0.152658622 0   0.6 NA  10  140 30  ((alpha*speed^5) beta*speed^4 gamma*speed^3 delta*speed^2 epsilon*speed zeta)*(theta)
0.524738843 0   -0.01   0   9.36E-05    0   0   0   0   5   130 30  ((alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2))*(1-RF)
0.283553945 -0.0234 -0.00869    0.000443    0.000114    0   0   0   0   5   130 30  ((alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2))*(1-RF)
0.189162424 1.571506423 0.081547506 0.027327758 -0.000248892    -0.268467236    0   0   0   5   130 30  (alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2)
0.474232144 5.621746462 0.340577763 0.083762882 -0.001515827    -1.191086695    0   0   0   5   130 30  (alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2)
0.189162424 1.571506423 0.081547506 0.027327758 -0.000248892    -0.268467236    0   0   0   5   130 30  (alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2)
>)

I have tried this but it's not working:

nox.file$calculated_col <- eval(parse(text=as.character(nox.file$equation)), nox.file)

I have also tried wrapping it in 'mutate' to help with vectorising it but neither does this work:

nox.file <- nox.file %>% mutate(calculated_col=eval(parse(text=as.character(equation)), nox.file))

Can anyone tell me what's wrong?

CodePudding user response:

You could also do:

df %>% 
   rowwise() %>%
   mutate(sol = eval(parse(text = equation)))

CodePudding user response:

The problem with your attempt is that eval is not vectorized. It only evaluates the last element of the expression vector parse(text = equation). You can try something like this instead:

library("dplyr")

dd <- nox.file %>% 
    group_by(equation) %>%
    ## mutate(calculated_col = eval(str2lang(cur_group()[[1L]])))
    mutate(calculated_col = tryCatch(eval(str2lang(cur_group()[[1L]])), error = function(e) NA_real_))

dd$calculated_col
## [1] 1.59730000 1.94060000 2.24640000 0.05175415 0.03528692
## [6] 0.30897884 0.18006882 0.03303038 0.03790261 0.03303038

I reconstructed nox.file from your copy and paste like so:

x <- "alpha beta    gamma   delta   epsilon zeta    eta theta   RF  min.speed.kph   max.speed.kph   speed   equation
9.70E-05    0.0026  1.432   0   0   0   0   0   0   10  130 30  ((alpha*speed^2) (beta*speed) gamma (delta*log10(speed)) (epsilon*exp(zeta*speed)) (eta*(speed^theta)))*(1-RF)
7.40E-05    0.013   1.484   0   0   0   0   0   0   10  130 30  ((alpha*speed^2) (beta*speed) gamma (delta*log10(speed)) (epsilon*exp(zeta*speed)) (eta*(speed^theta)))*(1-RF)
0.000266    -0.014  2.427   0   0   0   0   0   0   10  130 30  ((alpha*speed^2) (beta*speed) gamma (delta*log10(speed)) (epsilon*exp(zeta*speed)) (eta*(speed^theta)))*(1-RF)
2.88E-12    2.56E-10    -2.09E-07   7.75E-05    -0.005274381    0.152658622 0   0.88    NA  10  140 30  ((alpha*speed^5) beta*speed^4 gamma*speed^3 delta*speed^2 epsilon*speed zeta)*(theta)
2.88E-12    2.56E-10    -2.09E-07   7.75E-05    -0.005274381    0.152658622 0   0.6 NA  10  140 30  ((alpha*speed^5) beta*speed^4 gamma*speed^3 delta*speed^2 epsilon*speed zeta)*(theta)
0.524738843 0   -0.01   0   9.36E-05    0   0   0   0   5   130 30  ((alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2))*(1-RF)
0.283553945 -0.0234 -0.00869    0.000443    0.000114    0   0   0   0   5   130 30  ((alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2))*(1-RF)
0.189162424 1.571506423 0.081547506 0.027327758 -0.000248892    -0.268467236    0   0   0   5   130 30  (alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2)
0.474232144 5.621746462 0.340577763 0.083762882 -0.001515827    -1.191086695    0   0   0   5   130 30  (alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2)
0.189162424 1.571506423 0.081547506 0.027327758 -0.000248892    -0.268467236    0   0   0   5   130 30  (alpha gamma*speed epsilon*speed^2 zeta/speed)/(1 beta*speed delta*speed^2)"

con <- textConnection(x)
nox.file <- read.table(con, header = TRUE)
close(con)

The reconstruction may not be exact, which is why you are encouraged to include proper dput output in your question.

  • Related