[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  Logic: Logical Operators

##  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

x, y

` [ raw ](/link/raw?package=base&version=3.6.2) ` , ` [ logical
](/link/logical?package=base&version=3.6.2) ` or ‘number-like’ vectors (i.e.,
of types ` [ double ](/link/double?package=base&version=3.6.2) ` (class ` [
numeric ](/link/numeric?package=base&version=3.6.2) ` ), ` [ integer
](/link/integer?package=base&version=3.6.2) ` and ` [ complex
](/link/complex?package=base&version=3.6.2) ` ), or objects for which methods
have been written.

##  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 ](/link/warning?package=base&version=3.6.2) ` 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 ](/link/tsp?package=base&version=3.6.2) ` 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 ](/link/Logic?package=base&version=3.6.2&to=%3DS4groupGeneric) ` group generic (and hence methods need argument names ` e1, e2 ` ). 

##  Details

` ! ` indicates logical negation (NOT).

` & ` and ` && ` indicate logical AND and ` | ` and ` || ` indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in ` [ if ](/link/if?package=base&version=3.6.2) ` 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 to ` if(cond) ` because of ` [ NA
](/link/NA?package=base&version=3.6.2) ` 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 ` ! ` , ` & ` , ` | ` and ` xor ` , 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 ](/link/Ops?package=base&version=3.6.2&to=%3DS3groupGeneric) ` (or S4 ` Logic ` , see below) group generic function. (See ` [ Ops ](/link/Ops?package=base&version=3.6.2&to=%3DS3groupGeneric) ` for how dispatch is computed.) 

` [ NA ](/link/NA?package=base&ver