46

Why there are four logical operators:

&, &&
|, ||

What's the differences in usage?

Yes, I've checked the docs, yet I'm a little bit confused. The docs says:

 ‘&’ 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.

I think a piece of example will clearly demonstrate them. Thanks.

Nick
  • 8,451
  • 13
  • 57
  • 106
  • 16
    Would be nice to include the duplicate link... – Michael Szczepaniak May 31 '17 at 02:48
  • 1
    Using dplyr, the & and | logical operators are used. I have accidentally used && and II many times (because I am also a C# programmer) and it returns the incorrect results that one would expect from using the logical AND and OR. – Andrew Borst Sep 08 '20 at 13:07
  • I am glad you asked. I am almost always confused with the R docs. – eod Jan 31 '23 at 11:12
  • & may return a vector, and && returns a single TRUE or FALSE. I would assume the same is true for | vs. ||. Clear answer for difference between & and && is here https://stackoverflow.com/a/6559049/6136776 – eod Jan 31 '23 at 11:21

1 Answers1

38

Key differences are as below...

  1. Long form(&& or ||) short circuits, which means if it can identify the result by just validating just the first element. While doing &&, if the comparision of first two elements resulted in false, comparing next set of elements will also result in False. So, it returns false. While doing || if comparision resulted in true in first few elements, we can confidently say that any further validations will not change the result so it returns True.

  2. Short forms continues to do for the entire vectors and creates a vector of results and returns it.

Hope this helps.

& 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.

Source: http://stat.ethz.ch/R-manual/R-patched/library/base/html/Logic.html

Buddha
  • 4,339
  • 2
  • 27
  • 51
  • 6
    This text is present in the question! – Matthew Lundberg Apr 16 '13 at 02:57
  • 7
    @MatthewLundberg This text has been added after I answered. – Buddha Apr 16 '13 at 03:01
  • 8
    The question shows no edits, but that happens -- quick edits get rolled into one, and leave quick answers looking foolish. I considered downvoting your answer, and am now quite happy that I did not. – Matthew Lundberg Apr 16 '13 at 03:18
  • Here are some examples. Warning - login to coursera required. [More on R operators | Google Data Analytics Professional Certificate](https://www.coursera.org/learn/data-analysis-r/supplement/n5cto/more-on-r-operators) – Pavol Travnik Nov 24 '22 at 09:34
  • After reading the answer, I am still confused. Couldn't you give 4 examples? – eod Jan 31 '23 at 11:15