Learn R Programming

mark (version 0.8.1)

logic_ext: Logic - Extension'

Description

All functions take logical or logical-like (i.e., 1, 0, or NA as integer or doubles) and return logical values.

Extensions to the base logical operations to account for NA values.

base::isTRUE() and base::isFALSE() will only return single length TRUE or FALSE as it checks for valid lengths in the evaluation. When needing to check over a vector for the presence of TRUE or FALSE and not being held back by NA values, is_true and is_false will always provide a TRUE FALSE when the vector is logical or return NA is the vector x is not logical.

%or% is just a wrapper for base::xor()

Usage

is_true(x)

# S3 method for default is_true(x)

# S3 method for logical is_true(x)

is_false(x)

# S3 method for default is_false(x)

# S3 method for logical is_false(x)

x %xor% y

OR(..., na.rm = FALSE)

AND(..., na.rm = FALSE)

either(x, y)

is_boolean(x)

none(..., na.rm = FALSE)

Value

  • is_true(), is_false(), either(), %or%, AND(), OR(): A logical vector, equal length of x (or y or of all ... lengths)

  • is_boolean(): TRUE or FALSE

  • none(): TRUE, FALSE, or NA

Arguments

x, y

A vector of logical values. If NULL will generate a warning. If not a logical value, will return NA equal to the vector length

...

Vectors or a list of logical values

na.rm

Logical, if TRUE will ignore NA

Details

Logical operations, extended

Examples

Run this code
x <- c(TRUE, FALSE, NA)
y <- c(FALSE, FALSE, TRUE)
z <- c(TRUE, NA, TRUE)
isTRUE(x)
is_true(x)
isFALSE(x)
is_false(x)
x %xor% TRUE
TRUE %xor% TRUE
TRUE %xor% FALSE
NA %xor% FALSE
OR(x, y, z)
OR(x, y, z, na.rm = TRUE)
AND(x, y, z)
AND(x, y, z, na.rm = TRUE)
either(x, FALSE)
either(TRUE, FALSE)
either(FALSE, NA)
either(TRUE, NA)
none(x)
none(x & y, na.rm = TRUE)
is_boolean(x)
is_boolean(c(1L, NA_integer_, 0L))
is_boolean(c(1.01, 0, -1))

Run the code above in your browser using DataLab