R: Logical Operators (original) (raw)

Logic {base} R Documentation

Description

These operators act on raw, logical and number-like vectors.

Usage

! x
x & y
x && y
x | y
x || y
xor(x, y)

isTRUE (x)
isFALSE(x)

Arguments

Details

! indicates logical negation (NOT).

& and && indicate logical AND and | and ||indicate logical OR. The shorter forms performs elementwise comparisons in much the same way as arithmetic operators. The longer forms evaluates left to right, proceeding only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in [if](../../base/help/if.html) clauses.

Using vectors of more than one element in && or || will give an error.

xor indicates elementwise exclusive OR.

isTRUE(x) is the same as{ is.logical(x) && length(x) == 1 && !is.na(x) && x };isFALSE() is defined analogously. Consequently,if(isTRUE(cond)) may be preferable to if(cond) because of [NA](../../base/help/NA.html)s.
In earlier R versions, isTRUE <- function(x) identical(x, TRUE), had the drawback to be false e.g., for x <- c(val = TRUE).

Numeric and complex vectors will be coerced to logical values, with zero being false and all non-zero values being true. Raw vectors are handled without any coercion for !, &, | andxor, with these operators being applied bitwise (so ! is the 1s-complement).

The operators !, & and | are generic functions: methods can be written for them individually or via the[Ops](../../base/help/S3groupGeneric.html) (or S4 Logic, see below) group generic function. (See [Ops](../../base/help/S3groupGeneric.html) for how dispatch is computed.)

[NA](../../base/help/NA.html) is a valid logical object. Where a component ofx or y is NA, the result will be NA if the outcome is ambiguous. In other words NA & TRUE evaluates toNA, but NA & FALSE evaluates to FALSE. See the examples below.

See Syntax for the precedence of these operators: unlike many other languages (including S) the AND and OR operators do not have the same precedence (the AND operators have higher precedence than the OR operators).

Value

For !, a logical or raw vector(for raw x) of the same length as x: names, dims and dimnames are copied from x, and all other attributes (including class) if no coercion is done.

For |, & and xor a logical or raw vector. If involving a zero-length vector the result has length zero. Otherwise, the elements of shorter vectors are recycled as necessary (with a[warning](../../base/help/warning.html) when they are recycled only fractionally). The rules for determining the attributes of the result are rather complicated. Most attributes are taken from the longer argument, the first if they are of the same length. Names will be copied from the first if it is the same length as the answer, otherwise from the second if that is. For time series, these operations are allowed only if the series are compatible, when the class and [tsp](../../stats/html/tsp.html)attribute of whichever is a time series (the same, if both are) are used. For arrays (and an array result) the dimensions and dimnames are taken from first argument if it is an array, otherwise the second.

For ||, && and isTRUE, a length-one logical vector.

S4 methods

!, & and | are S4 generics, the latter two part of the [Logic](../../methods/html/S4groupGeneric.html) group generic (and hence methods need argument names e1, e2).

Note

The elementwise operators are sometimes called as functions as e.g. `&`(x, y): see the description of how argument-matching is done in [Ops](../help/groupGeneric.html).

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language. Wadsworth & Brooks/Cole.

See Also

[TRUE](../../base/help/TRUE.html) or [logical](../../base/help/logical.html).

[any](../../base/help/any.html) and [all](../../base/help/all.html) for OR and AND on many scalar arguments.

[Syntax](../../base/help/Syntax.html) for operator precedence.

L [%||%](../../base/help/+25+7C+7C+25.html) R which takes L if it is not NULL, and R otherwise.

[bitwAnd](../../base/help/bitwAnd.html) for bitwise versions for integer vectors.

Examples

y <- 1 + (x <- stats::rpois(50, lambda = 1.5) / 4 - 1)
x[(x > 0) & (x < 1)]    # all x values between 0 and 1
if (any(x == 0) || any(y == 0)) "zero encountered"

## construct truth tables :

x <- c(NA, FALSE, TRUE)
names(x) <- as.character(x)
outer(x, x, `&`) ## AND table
outer(x, x, `|`) ## OR  table

[Package _base_ version 4.6.0 Index]