I have a problem with an ifelse evaluation.
The following function evaluates based on 3 conditions:
mk <- function(a, b, c, d, e_1, e_2, f, k)
# condition 1
ifelse (!is.na(e_1) & !(k %in% 1),
mk <- d - e_1 * c,
# condition 2
ifelse (!is.na(e_2) & !(k %in% 1),
mk <- e_2 - d * c,
# condition 3
ifelse((a - b) <= 11,
mk <- c * a - b * f,
mk <- c * f
))
)
if I parse a single element the function evaluates correctly, but if I give rows of a dataframe as input values the function only ever uses the computation in the last condition, even if the previous conditions are met. the columns containing the values for e_1, e_2 and k have some NA's in them, I suspect that is the problem. what I don't get is why the NA'S force the whole vector to be evaluated as condition 3, even if they are actually never used in the computation because the conditions should rule out their usage. if I replace the calculations with characters, i.e. write "uses condition 1/2/3" instead of the formulas, the conditions are evaluated correctly.
how can I avoid this problem?
e_2
are notNA
anda-b <= 11
? - John Pauldata.frame
to the function? This is hard to test without that and some data. - John Paul