Logic
Logical Operators

### Description

Operators work on logical or number-like vectors. The operators are & (and), | (or), ! (not), or xor(x,y) (exclusive or) .

### Usage

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

### Arguments

x, y | a logical or number-like vector of objects. The mode of a number-like vector could be "integer", "double" or "raw". Missing values (NAs) are allowed.

### Details

Results corresponding to NA in the operands will be either a logical value or an NA, depending on whether the other operand uniquely determines the answer. For example, NA|TRUE must be TRUE, and NA&FALSE must be FALSE. See section 5.1.5 of Becker, Chambers and Wilks (1988) for the rules for dealing with operands with attributes.
All number-like non-raw vectors will be coerced to logical: zero value will be FALSE and non-zero value will be TRUE.
All raw vectors will not be coerced, and will return the result of bitwise operation.
Logic is a group generic function, including a subset of functions included under the Ops group generic function. You can find the current list of functions in this group by typing getGroupMembers("Logic").
Value
returns a logical vector of using & (and), | (or), ! (negation), or xor(x, y) (exclusive or) if x, y are logical vectors or non-raw number-like vectors. If the number-like vector has the mode "raw", it returns a raw vector containing the result of a bitwise operation. The result is as long as the longer of the operands for all but negation.
Classes
The !, &, and | operators make up the Logic group of operators within the Ops group.
References
Becker, R.A., Chambers, J.M., and Wilks, A.R. (1988). The New S Language Wadsworth and Brooks/Cole, Pacific Grove, CA.
Differences between Spotfire Enterprise Runtime for R and Open-source R
In open-source R, the negation (!) operator is not a group member of Logic.
See Also
if for || (sequential or) and && (sequential and). Compare, Groups, Ops, and individual functions listed below.
Examples

    # The Ops group of generic functions includes:
    #   Arithmetic:  +, -, *, /, ^, %/%, %%
    #   Comparison:  >, <, >=, <=, ==, !=, compare
    #   Logical:     !, &, |
    # See also .Uminus and xor
    cu <- Sdatasets::cu.summary
    # Cars made in USA or Japan or both:
    cu[cu$Country=="USA"
        | cu$Country=="Japan/USA"
        | cu$Country == "Japan", ]
    # Rows of cu.summary with no NA's in either Reliability or Mileage:
    cu[!is.na(cu$Reliability) & !is.na(cu$Mileage), ]
    # Cars with Mileages outside the interquartile range exclusive of NA's:
    safe <- !is.na(cu$Mileage)
    cu[xor(cu$Mileage > 21,
        cu$Mileage < 27) & safe, ]
    #  Logic tables for &, | and xor:
    a <- c(TRUE, FALSE, NA)
    outer(a, a,"&")
    outer(a, a, "|")
    outer(a, a, "xor")
    # Other "numeric" type
    e1 <- c(1, 3, 0, 3, NA, 0)
    e2 <- c(3, 0, 0, 2, -5, NA)
    e1 & e2  # Result:  TRUE FALSE FALSE  TRUE    NA  FALSE
    e1 | e2   # Result:  TRUE  TRUE FALSE  TRUE  TRUE    NA
    !e1    # Result: FALSE FALSE  TRUE FALSE    NA  TRUE
    xor(e1, e2) # Result: FALSE  TRUE FALSE FALSE    NA    NA
    #  "raw" type, the shorter length of vector is recycled.
    e1.raw <- as.vector(c(42, 54, 13, 0, 18, 25), mode = "raw")
    e2.raw <- as.vector(c(17, 0, 5, NA), mode = "raw")
    e1.raw & e2.raw  # Result:  00 00 05 00 10 00
    e1.raw | e2.raw   # Result:  3b 36 0d 00 13 19
    !e1.raw # Result: d5 c9 f2 ff ed e6
    xor(e1.raw, e2.raw)  # Result:  3b 36 08 00 03 19

Package base version 6.1.5-18
Package Index