Logical condition while subsetting not giving correct values

爷,独闯天下 提交于 2019-12-02 09:32:31

From ?base::Logic, help('&'), help('|'), etc

See Syntax for the precedence of these operators: unlike many other languages (including S) the AND and OR operators do not have the same precedence (the AND operators have higher precedence than the OR operators).

which explains why

TRUE | TRUE & FALSE
# [1] TRUE

which is essentially

TRUE | (TRUE & FALSE)

which is also true, and a simplification of what you are doing here:

(project$DC31==1&project$D14==2) |
  (project$DC31==2&project$D14==1) &
  !is.na(project$DC31) &
  !is.na(project$D14) &
  project$ROLL.NO. == 3131

since you expect the result only to contain some project$ROLL.NO. == 3131 I assume, so even if some of these are false, if one or more OR is true, you may get some that are not ROLL.NO. which are not 3131

Also note that ! has a higher precedence than logicals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!