1

Does anyone know of a way to use an equation that is given as a string as an actual equation or formula in R?

I have a database with allometric equations (that you can use to calculate/estimate biomass of trees), together with information on locations and species for which they were developed. I also have another large observed dataset with observed species; so I loop over all these species and their location to find the allometric equation for each, and the equations can be very different [this is just to indicate that this is not a once-off exercise so I cannot use a very specific approach]. I then want to use my observed dataset (with things like diameter and height of the trees) to calculate biomass - but I don't know how to actually use the expression that is in the allometric equation database. See below a very short example.

X   Z   Equation                                    Genus       Species
DBH NA  0.1731*X^(2.0296)                           All         All
DBH NA  0.1027*X^(2.4798)                           All         All
DBH H   (-4.58643+1.90532*log10(X)+1.0646*log10(Z)) Tectona     grandis
DBH H   (-4.58643+1.90532*log10(X)+1.0646*log10(Z)) Tectona     grandis
DBH NA  0.000189*X^(2.262)                          Dalbergia   melanoxylon
DBH H   0.0763*X^(2.2046)*Z^(0.4918)                All         All

So let's say I have Tectona grandis in my observed data, I want to be able to extract -4.58643+1.90532*log10(X)+1.0646*log10(Z) and use it with my observed X (diameter) and Z (height).

I need some sort of "generic" solution since I am dealing with a lot of species and a lot of different equations. I know how to select the actual equation, but then I have it as a factor - how do I operationalize it? Can I?

d.b
  • 32,245
  • 6
  • 36
  • 77
Geraldine
  • 771
  • 3
  • 9
  • 23
  • Are the actual `X` and `Z` numeric values in the same dataframe? Or are you looking them up somewhere else? It would help if you included the numeric `X` and `Z` in your example. – Marius Aug 31 '17 at 02:54
  • Yes, X and Z are tree diameter and height that are recorded in the observed data (which is separate from this database), as numeric values. I don't think it's an issue for me to actually extract those and use them in an equation – Geraldine Aug 31 '17 at 02:59
  • 2
    Test this: `x = "-4.58643+1.90532*log10(X)+1.0646*log10(Z)"; X = 2; Z = 5; eval(parse(text = x))` – d.b Aug 31 '17 at 03:00
  • 1
    Awesome, that works! Can you add that as an answer so I can upvote it and resolve it as the answer? I didn't know it was that easy! – Geraldine Aug 31 '17 at 03:08

1 Answers1

2

You can use eval and parse together to convert character to expressions and then evaluate it. I should note that it is not considered a good practice. However, here is an example that you can try:

#Your equation
x = "-4.58643+1.90532*log10(X)+1.0646*log10(Z)"

#Values you want to plug into x
X = 2
Z = 5

#Evaluate x
eval(parse(text = x))
#[1] -3.268748
d.b
  • 32,245
  • 6
  • 36
  • 77
  • 1
    Thanks! Out of interest - why is it not considered good practice? – Geraldine Aug 31 '17 at 03:29
  • 1
    @Geraldine, there are several questions in SO about that. Here's one: [What specifically are the dangers of eval(parse(…))?](https://stackoverflow.com/q/13649979/7128934) – d.b Aug 31 '17 at 03:31