1) Your criterion test.df$a >=4 & !is.na(test.df$b) & test.df$c > 4
evals to:
[1] FALSE TRUE NA FALSE TRUE NA FALSE TRUE
As documented, subset
will filters out the rows (3 and 6) where the criterion evals to NA
. On the other hand, [
gives you a row of NA
s for these as it is unsure if they should be included (TRUE
) or excluded (FALSE
).
2) I would use transform
and an improved criterion:
test.df <- transform(test.df, d = ifelse(!is.na(a) &
!is.na(b) &
!is.na(c) &
a >= 4 &
c > 4, 10, d))