naCompare: Compare values using <, <=, >, >=, !=, and == (robust to NAs)

naCompareR Documentation

Compare values using <, <=, >, >=, !=, and == (robust to NAs)

Description

This function and set of operators perform simple (vectorized) comparisons using <, <=, >, >=, !=, or == between values and always returns TRUE or FALSE. TRUE only occurs if the condition can be evaluated and it is TRUE. FALSE is returned if the condition is FALSE or it cannot be evaluated.

Usage

naCompare(op, x, y)

x %<na% y

x %<=na% y

x %==na% y

x %!=na% y

x %>na% y

x %>=na% y

Arguments

op

Character, the operation to perform: '<', '<=', '>', '>=', '!=', or '=='. Note this must be a character (i.e., put it in quotes).

x, y

Vectors of numeric, character, NA, and/or NaN values. This is the first value in the operation x XXX y where XXX is the operator in op. If x is shorter than y then x is recycled.

Value

Vector of logical values.

Examples

naCompare('<', c(1, 2, NA), c(10, 1, 0))
naCompare('<', c(1, 2, NA), 10)
naCompare('<', c(1, 2, NA), NA)
# compare to:
NA < 5
NA < NA

# same operations with operators:
1 %<na% 2
1 %<na% NA
3 %==na% 3
NA %==na% 3
4 %!=na% 4
4 %!=na% NA
5 %>=na% 3
5 %>=na% NA
3 %==na% c(NA, 1, 2, 3, 4)

# compare to:
1 < 2
1 < NA
3 == 3
NA == 3
4 != 4
4 != NA
5 >= 3
5 >= NA
3 == c(NA, 1, 2, 3, 4)

omnibus documentation built on Sept. 17, 2024, 5:09 p.m.