R gotcha: logical-and operator for combining conditions is & not &&
Solution 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.
Solution 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.
Comments
-
smci almost 2 years
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)
-
smci over 12 yearsYeah, I had specifically checked the documentation and it sends you the wrong way ('pairwise and' instead of 'bitwise'?) This is a very eccentric choice wrt almost every other language...
-
Andrie over 12 years@smci pairwise cf. element-wise. Not really that eccentric, given that you need a vectorised form of the
&
operator. -
smci about 10 years@Andrie: remember that in C/C++, Python, PHP and most other languages, '&' is the bitwise-and operator and '&&' is the logical-and. The R documentation shouldn't pretend we live in a vacuum. Any cases where R decides to be eccentric like this should be called out very clearly in documentation. I thunk between R and Python on a weekly basis.
-
Matt Chambers over 9 yearsYes, I also just got dinged by this quirk. I think R should have gone with &&& as the element-wise logical or, and kept & as bitwise (possibly element-wise bitwise).
-
smci over 8 years@MattChambers: if R also added a bitwise operator, and used '&' for it, I would put a (disableable) default warning on it "Warning:
&
is the bitwise-and operator, not logical-and. Is that what you meant to use?"