# # The logical class

Logical is a mode (and an implicit class) for vectors.

## # Logical operators

There are two sorts of logical operators: those that accept and return vectors of any length (elementwise operators: `!`, `|`, `&`, `xor()`) and those that only evaluate the first element in each argument (`&&`, `||`). The second sort is primarily used as the `cond` argument to the `if` function.

Logical Operator Meaning Syntax
! Not !x
& element-wise (vectorized) and x & y
&& and (single element only) x && y
element-wise (vectorized) or
xor element-wise (vectorized) exclusive OR xor(x,y)

Note that the `||` operator evaluates the left condition and if the left condition is TRUE the right side is never evaluated. This can save time if the first is the result of a complex operation. The `&&` operator will likewise return FALSE without evaluation of the second argument when the first element of the first argument is FALSE.

``````> x <- 5
> x > 6 || stop("X is too small")
Error: X is too small
> x > 3 || stop("X is too small")
 TRUE

``````

To check whether a value is a logical you can use the `is.logical()` function.

## # Coercion

To coerce a variable to a logical use the `as.logical()` function.

``````> x <- 2
> z <- x > 4
> z
 FALSE
> class(x)
 "numeric"
> as.logical(2)
 TRUE

``````

When applying `as.numeric()` to a logical, a double will be returned. `NA` is a logical value and a logical operator with an `NA` will return `NA` if the outcome is ambiguous.

## # Interpretation of NAs

See Missing values for details.

``````> TRUE & NA
 NA
> FALSE & NA
 FALSE
> TRUE || NA
 TRUE
> FALSE || NA
 NA

``````

### # Shorthand

`TRUE`, `FALSE` and `NA` are the only values for logical vectors; and all three are reserved words. `T` and `F` can be shorthand for `TRUE` and `FALSE` in a clean R session, but neither `T` nor `F` are reserved, so assignment of non-default values to those names can set users up for difficulties.