21

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
  • 26,085
  • 16
  • 96
  • 138
  • 1
    Also see a similar question at http://stackoverflow.com/a/6559049/210673 – Aaron left Stack Overflow Oct 20 '12 at 00:46
  • Thanks Aaron, but that one is immune to keyword searches, and you can't search on operators (punctuation). It was also not tagged properly (just fixed that), so that's two reasons it would never show up in a search. – smci Oct 20 '12 at 01:36
  • 1
    Thanks for fixing the tags. This is a hard topic to search for. Hopefully linking the questions together will help in finding helpful answers too. – Aaron left Stack Overflow Oct 21 '12 at 20:15

2 Answers2

26

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.

Andrie
  • 163,419
  • 39
  • 422
  • 472
8

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.

Gavin Simpson
  • 157,540
  • 25
  • 364
  • 424
James Thompson
  • 43,044
  • 17
  • 61
  • 80
  • 1
    Yeah, 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... – smci Aug 03 '11 at 21:39
  • @smci pairwise cf. element-wise. Not really that eccentric, given that you need a vectorised form of the `&` operator. – Andrie Aug 03 '11 at 21:48
  • 2
    @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. – smci Feb 18 '14 at 00:42
  • 1
    Yes, 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). – Matt Chambers Aug 27 '14 at 17:10
  • @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?"* – smci Sep 22 '15 at 21:06