Differences between vectors _including_ NA

Suppose I have a vector x<-c(1,2,NA,4,5,NA) .

I apply some mythological code to this vector, which leads to another vector y<-c(1,NA,3, 4,10,NA)

Now I want to find out in which positions two vectors separate me, where I consider two NA same and one NA and not NA (for example, the second element from two examples is vectors).

In particular, for my example, I would like to get a vector holding c(2,3,5) .

In my use case, I am not happy with the vector of boolean variables, but obviously I can easily convert ( which ), so I also agree with that.

I have some solutions like:

 simplediff<-x!=y nadiff<-is.na(x)!=is.na(y) which(simplediff | nadiff) 

but it seems to me that I am reinventing the wheel here. Any better options?

+6
source share
2 answers

How about looping and using identical ?

  !mapply(identical,x,y) [1] FALSE TRUE TRUE FALSE TRUE FALSE 

And for the positions:

 seq_along(x)[!mapply(identical,x,y)] [1] 2 3 5 

or

 which(!mapply(identical,x,y)) [1] 2 3 5 
+6
source

One possible solution (but this is not the best):

 (1:length(x))[-which((xy)==0)] 
0
source

Source: https://habr.com/ru/post/902831/


All Articles