R gotcha: logical-and operator for combining conditions is & not &&

非 Y 不嫁゛ 提交于 2019-11-26 08:24:39

问题


Why doesn\'t subset() work with a logical and && operator combining two conditions?

> subset(tt, (customer_id==177 && visit_date==\"2010-08-26\"))
<0 rows> (or 0-length row.names)

but they each work individually:

> subset(tt, customer_id==177)

> subset(tt, visit_date==\"2010-08-26\")

(Want to avoid using large temporary variables - my dataset is huge)


回答1:


From the help page for Logical Operators, accessible by ?"&&":

& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.

(R version 2.13-0)

In other words, when using subset, use the single &.


Here is an illustration of the difference:

c(1,1,0,0) & c(1,0,1,0)
[1]  TRUE FALSE FALSE FALSE

c(1,1,0,0) && c(1,0,1,0)
[1] TRUE

If this looks quirky compared to other programming paradigms, remember that R needs to provide a vectorised form of the operator.




回答2:


In R, you actually want the & operator rather than && to do a pairwise AND operation, the && does a bitwise AND. The same rule applies for OR: if you want to do a logical OR rather than a bitwise OR, you want the | operator.



来源:https://stackoverflow.com/questions/6933598/r-gotcha-logical-and-operator-for-combining-conditions-is-not

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