# Strange Error in if/conditional statement

Hi there,

I am trying to use elements from a vector to conduct if-then statements, but for some reason, I keep running into the following error:

"Error in if (xd <= a) { : missing value where TRUE/FALSE needed"

I am not sure why this keeps coming up, as the only thing that is different with that line of code seems to be that I am using xd instead of xd. I get my desired results with xd but not xd. The conditional seems to be met as well.

``````reset1 = {

a = 0.3 #lower bound of belief

b = 0.9 #upper bound of belief

A = 5

w = c(1,2)

xd = c(1,2)

w = 5   #cost of abatement/effort for Driver1

xd = 0.3

w = 3   #cost of abatement/effort for Driver2

xd = 0.3

expfine1 = function(xd,A,a,b){if(xd<=a){A} else if(a<xd & xd<b){(((b-xd)/(b-a))*A)} else if(xd>b){0}}
expfine1(xd,A,a,b)

expcost1 = function(xd){proba1(xd)*expfine1(xd,A,a,b)}
expcost1(xd)

expfine2 = function(xd,A,a,b){if(xd<=a){A} else if(a<xd & xd<b){(((b-xd)/(b-a))*A)} else if(xd>b){0}}
expfine2(xd, A, a, b)
expcost2 = function(xd){proba2(xd)*expfine2(xd,A,a,b)}
expcost2(xd)
``````

``````expfine2 = function(xd,A,a,b)
``````

so inside of the function xd will refer to whatever is passed to that parameter. You then call the function with

``````expfine2(xd, A, a, b)
``````

so xd within the function refers to the single value of xd. When you attempt the comparison

``````a < xd
``````

xd returns NA. This would be easier to think about if you started the definition of your function with

``````expfine2 = function(K, L, M, N)
``````

If you pass xd to K, then K returns the value of xd and K returns NA. Here is an example with no function involved.

``````xd <- c(3, 9)
TEST <- xd
TEST
#>  9
TEST
#>  NA
``````

Created on 2020-02-21 by the reprex package (v0.2.1)

3 Likes