Learn R Programming

omnibus (version 1.2.12)

naCompare: 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 %

x %<=na% y<="" p="">

x %==na% y

x %!=na% y

x %>na% y

x %>=na% y

Value

Vector of logical values.

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.

Examples

Run this code
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% 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)

Run the code above in your browser using DataLab