I need to create a new variable in my data frame that is the output of an equation with many nested parentheses. Part of this equation is in the form of the last line below
temp=36
Tc = 647.097
( ( 1-273.15+temp )/Tc )^1.5
where temp
will be a variable, and Tc
will be a constant. However, when I run the code the result is always NA
.
But, if I break the code down into the number that I know results from
( 1-273.15+temp )/Tc
and then add the exponent like so
-0.3649376^1.5
then the code works as it should.
Why is R not able to correctly output the calculation ( ( 1-273.15+temp )/Tc )^1.5
?
And more importantly, how can I get R to give me the result of ( ( 1-273.15+temp )/Tc )^1.5
while retaining my use of objects for the constant and the variable?
I need to address this, because the full equation is even worse, where the problem I describe above is nested within itself:
e_sat_test <- Pc^( ( Tc/(273.15+temp ) ) *
( a1*( (1-273.15+temp)/Tc ) + a2*( (1-273.15+temp)/Tc )^1.5 +
a3*( (1-273.15+temp)/Tc )^3 + a4* ( (1-273.15+temp)/Tc )^3.5 +
a5*( (1-273.15+temp)/Tc)^4 + a6*( (1-273.15+temp)/Tc )^7.5 ) )
The problem is that
-0.3649376^1.5
is interpreted as
-(0.3649376^1.5)
not
(-0.3649376)^1.5
because the exponent operator has a higher precedence. And when you take something to a .5 exponent that's like taking a square root and those aren't defined for simple numeric vectors in R (unless you want to use imaginary numbers). Your calculation is simply NaN for your data values because your result is not real. You might want to check your formula again,