Rev 82652 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/Logic.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2022 R Core Team% Distributed under GPL 2 or later\name{Logic}\title{Logical Operators}\alias{!}\alias{&}\alias{&&}\alias{|}\alias{||}\alias{xor}\alias{Logic}\alias{isTRUE}\alias{isFALSE}\usage{! xx & yx && yx | yx || yxor(x, y)isTRUE (x)isFALSE(x)}\description{These operators act on raw, logical and number-like vectors.}\arguments{\item{x, y}{\code{\link{raw}}, \code{\link{logical}} or \sQuote{number-like} vectors (i.e., oftypes \code{\link{double}} (class \code{\link{numeric}}),\code{\link{integer}} and \code{\link{complex}}), or objects forwhich methods have been written.}}\details{\code{!} indicates logical negation (NOT).\code{&} and \code{&&} indicate logical AND and \code{|} and \code{||}indicate logical OR. The shorter forms performs elementwisecomparisons in much the same way as arithmetic operators. The longerforms evaluates left to right, proceeding only until the result isdetermined. The longer form is appropriate for programmingcontrol-flow and typically preferred in \code{\link{if}} clauses.Using vectors of more than one element in \code{&&} or \code{||} willgive an error.\code{xor} indicates elementwise exclusive OR.\code{isTRUE(x)} is the same as\code{{ is.logical(x) && length(x) == 1 && !is.na(x) && x }};\code{isFALSE()} is defined analogously. Consequently,\code{if(isTRUE(cond))} may be preferable to \code{if(cond)} becauseof \code{\link{NA}}s.\crIn earlier \R versions, \code{isTRUE <- function(x) identical(x, TRUE)},had the drawback to be false e.g., for \code{x <- 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 \code{!}, \code{&}, \code{|} and\code{xor}, with these operators being applied bitwise (so \code{!} isthe 1s-complement).The operators \code{!}, \code{&} and \code{|} are generic functions:methods can be written for them individually or via the\code{\link[=S3groupGeneric]{Ops}} (or S4 \code{Logic}, see below)group generic function. (See \code{\link[=S3groupGeneric]{Ops}} forhow dispatch is computed.)\code{\link{NA}} is a valid logical object. Where a component of\code{x} or \code{y} is \code{NA}, the result will be \code{NA} if theoutcome is ambiguous. In other words \code{NA & TRUE} evaluates to\code{NA}, but \code{NA & FALSE} evaluates to \code{FALSE}. See theexamples below.See \link{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).}\value{For \code{!}, a logical or raw vector(for raw \code{x}) of the samelength as \code{x}: names, dims and dimnames are copied from \code{x},and all other attributes (including class) if no coercion is done.For \code{|}, \code{&} and \code{xor} 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 a\code{\link{warning}} when they are recycled only \emph{fractionally}).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 and \code{\link{tsp}}attribute 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 \code{||}, \code{&&} and \code{isTRUE}, a length-one logical vector.}\section{S4 methods}{\code{!}, \code{&} and \code{|} are S4 generics, the latter two partof the \code{\link[=S4groupGeneric]{Logic}} group generic (andhence methods need argument names \code{e1, e2}).}\note{The elementwise operators are sometimes called as functions ase.g.\sspace{}\code{`&`(x, y)}: see the description of howargument-matching is done in \code{\link[base:groupGeneric]{Ops}}.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.}\seealso{\code{\link{TRUE}} or \code{\link{logical}}.\code{\link{any}} and \code{\link{all}} for OR and AND on many scalararguments.\code{\link{Syntax}} for operator precedence.\code{\link{bitwAnd}} 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 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}\keyword{logic}