Movatterモバイル変換


[0]ホーム

URL:


rdrr.io

Logic: Logical Operators

LogicR Documentation

Logical Operators

Description

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

Usage

! xx & yx && yx | yx || yxor(x, y)isTRUE (x)isFALSE(x)

Arguments

x, y

raw,logical or ‘number-like’ vectors (i.e., oftypesdouble (classnumeric),integer andcomplex), or objects forwhich methods have been written.

Details

! 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).

Value

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.

S4 methods

!,& and| are S4 generics, the latter two partof theLogic group generic (andhence methods need argument namese1, e2).

Note

The elementwise operators are sometimes called as functions ase.g.`&`(x, y): see the description of howargument-matching is done inOps.

References

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

See Also

TRUE orlogical.

any andall for OR and AND on many scalararguments.

Syntax for operator precedence.

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

What can we improve?

R Package Documentation

Browse R Packages

We want your feedback!

Note that we can't provide technical support on individual packages. You should contact the package authors for that.

 
Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, readEmbedding Snippets.

Close

[8]ページ先頭

©2009-2026 Movatter.jp