Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find mismatch location across two vectors, including NAs

Tags:

r

match

vector

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.

Data example

> 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

What I want to get

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

Things I've tried:

> which(x != y)
integer(0)
> setdiff(x, y)
numeric(0)
> !(x %in% y)
[1] FALSE FALSE FALSE FALSE FALSE
like image 282
Lucas De Abreu Maia Avatar asked Mar 03 '23 19:03

Lucas De Abreu Maia


1 Answers

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
like image 189
Ronak Shah Avatar answered Mar 05 '23 15:03

Ronak Shah