What are the differences between & and &&, | and || in R?

Why there are four logical operators:

&, &&
|, ||

What are the differences in use?

Yes, I checked the documents, but I'm a little confused. The docs say:

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

I think that part of the example will demonstrate them. Thank.

+5
source share
1 answer

Hope this helps.

& and && indicate logical AND and | and || specify logical OR. the shorter form performs elementwise comparisons in much the same way as arithmetic operators. A longer form is evaluated from left to right, considering only the first element of each vector. Evaluation continues until a result is determined. A longer form is suitable for programming the control flow and is usually preferred in if sections.

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

+7

All Articles