Reputation: 11956
Suppose I have a vector x<-c(1,2,NA,4,5,NA)
.
I apply some mythological code to that vector, which results in another vector, y<-c(1,NA,3, 4,10,NA)
Now I wish to find out at which positions my two vectors differ, where I count two NA
s as being the same, and one NA
and a non-NA
(e.g. the second element of the two example vectors).
Specifically, for my example, I would like to end up with a vector holding c(2,3,5)
.
For my use case, I am not content with a vector of logical variables, but obviously I can easily convert (which
), so I'll accept that as well.
I have some solutions like:
simplediff<-x!=y
nadiff<-is.na(x)!=is.na(y)
which(simplediff | nadiff)
but it feels like I'm reinventing the wheel here. Any better options?
Upvotes: 6
Views: 2867
Reputation: 66834
How about looping and using identical
?
!mapply(identical,x,y)
[1] FALSE TRUE TRUE FALSE TRUE FALSE
And for positions:
seq_along(x)[!mapply(identical,x,y)]
[1] 2 3 5
or
which(!mapply(identical,x,y))
[1] 2 3 5
Upvotes: 6
Reputation: 2498
One posible solution (but sure it is not the best):
(1:length(x))[-which((x-y)==0)]
Upvotes: 0