equals2 {DIZtools} | R Documentation |
Robust compare two elements and return true if both elements are the same. False otherwise. No 'NA' or 'NULL'.
Description
The base-R function '==' is not working in an intended way for NAs and boolean. This function fixes this.
Usage
equals2(v1, v2)
Arguments
v1 |
First vector or element |
v2 |
Second vector or element |
Value
The equality between both vectors.
References
http://www.cookbook-r.com/Manipulating_data/ Comparing_vectors_or_factors_with_NA/>
Examples
## Not run:
dt <-
data.table::data.table(
a = c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, NA, NA, NA),
b = c(TRUE, FALSE, NA, TRUE, FALSE, NA, TRUE, FALSE, NA)
)
dt[, "classic_result" := get("a") == get("b")]
dt[, "result_expected" := equals2(get("a"), get("b"))]
dt
## This is the result:
# a b classic_result result_expected
# 1: TRUE TRUE TRUE TRUE
# 2: TRUE FALSE FALSE FALSE
# 3: TRUE NA NA FALSE
# 4: FALSE TRUE FALSE FALSE
# 5: FALSE FALSE TRUE TRUE
# 6: FALSE NA NA FALSE
# 7: NA TRUE NA FALSE
# 8: NA FALSE NA FALSE
# 9: NA NA NA TRUE
## End(Not run)
[Package DIZtools version 1.0.1 Index]