equals2 | R Documentation |
The base-R function '==' is not working in an intended way for NAs and boolean. This function fixes this.
equals2(v1, v2)
v1 |
First vector or element |
v2 |
Second vector or element |
The equality between both vectors.
http://www.cookbook-r.com/Manipulating_data/ Comparing_vectors_or_factors_with_NA/>
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.