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.
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/orNaN
values. This is the first value in the operationx XXX y
whereXXX
is the operator inop
. Ifx
is shorter thany
thenx
is recycled.
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