Skip to contents

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))
#> [1]  TRUE FALSE FALSE
naCompare('<', c(1, 2, NA), 10)
#> [1]  TRUE  TRUE FALSE
naCompare('<', c(1, 2, NA), NA)
#> [1] FALSE FALSE FALSE
# compare to:
NA < 5
#> [1] NA
NA < NA
#> [1] NA

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

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