I'm quite embarrassed to ask this question. An experienced R user should definitely know this, yet here goes:
I need to find the location of the mismatches between two supposedly identical vectors. It's very important in this case that NAs are considered mismatches.
> x <- c(1, NA, NA, NA, 5); x
[1] 1 NA NA NA 5
> y <- c(1, NA, NA, 4, 5); y
[1] 1 NA NA 4 5
Doesn't matter to me if I get a boolean vector or a numeric vector with the mismatch locations:
> c(F, T, T, T, F)
[1] FALSE TRUE TRUE TRUE FALSE
> 2:4
[1] 2 3 4
> which(x != y)
integer(0)
> setdiff(x, y)
numeric(0)
> !(x %in% y)
[1] FALSE FALSE FALSE FALSE FALSE
You can use :
x != y | is.na(x & y)
#Or checking NA individually
#x != y | is.na(x) | is.na(y)
#[1] FALSE TRUE TRUE TRUE FALSE
You can then easily wrap which
around this to get numeric vector
which(x != y | is.na(x & y))
#[1] 2 3 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With