| Logic | R Documentation |
These operators act on raw, logical and number-like vectors.
! xx & yx && yx | yx || yxor(x, y)isTRUE (x)isFALSE(x)
x, y |
|
! indicates logical negation (NOT).
& and&& indicate logical AND and| and||indicate logical OR. The shorter form performs elementwisecomparisons in much the same way as arithmetic operators. The longerform evaluates left to right examining only the first element of eachvector. Evaluation proceeds only until the result is determined. Thelonger form is appropriate for programming control-flow and typicallypreferred inif clauses.
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 toif(cond) becauseofNAs.
In earlierR versions,isTRUE <- function(x) identical(x, TRUE),had the drawback to be false e.g., forx <- c(val = TRUE).
Numeric and complex vectors will be coerced to logical values, withzero being false and all non-zero values being true. Raw vectors arehandled without any coercion for!,&,| andxor, with these operators being applied bitwise (so! isthe 1s-complement).
The operators!,& and| are generic functions:methods can be written for them individually or via theOps (or S4Logic, see below)group generic function. (SeeOps forhow dispatch is computed.)
NA is a valid logical object. Where a component ofx ory isNA, the result will beNA if theoutcome is ambiguous. In other wordsNA & TRUE evaluates toNA, butNA & FALSE evaluates toFALSE. See theexamples below.
See Syntax for the precedence of these operators: unlike manyother languages (including S) the AND and OR operators do not have thesame precedence (the AND operators have higher precedence than the ORoperators).
For!, a logical or raw vector(for rawx) of the samelength asx: names, dims and dimnames are copied fromx,and all other attributes (including class) if no coercion is done.
For|,& andxor a logical or raw vector. Ifinvolving a zero-length vector the result has length zero. Otherwise,the elements of shorter vectors are recycled as necessary (with awarning when they are recycled onlyfractionally).The rules for determining the attributes of the result are rathercomplicated. Most attributes are taken from the longer argument, thefirst if they are of the same length. Names will be copied from thefirst if it is the same length as the answer, otherwise from thesecond if that is. For time series, these operations are allowed onlyif the series are compatible, when the class andtspattribute of whichever is a time series (the same, if both are) areused. For arrays (and an array result) the dimensions and dimnamesare taken from first argument if it is an array, otherwise the second.
For||,&& andisTRUE, a length-one logical vector.
!,& and| are S4 generics, the latter two partof theLogic group generic (andhence methods need argument namese1, e2).
The elementwise operators are sometimes called as functions ase.g.`&`(x, y): see the description of howargument-matching is done inOps.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
TRUE orlogical.
any andall for OR and AND on many scalararguments.
Syntax for operator precedence.
bitwAnd for bitwise versions for integer vectors.
y <- 1 + (x <- stats::rpois(50, lambda = 1.5) / 4 - 1)x[(x > 0) & (x < 1)] # all x values between 0 and 1if (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 tableouter(x, x, "|") ## OR table
Add the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.