I've completely failed at searching for other r-help or Stack Overflow discussion of this specific issue. Sorry if it's somewhere obvious. I believe that I'm just looking for the easiest way to get R's == sign to never return NAs.
# Example #
# Say I have two vectors
a <- c( 1 , 2 , 3 )
b <- c( 1 , 2 , 4 )
# And want to test if each element in the first
# is identical to each element in the second:
a == b
# It does what I want perfectly:
# TRUE TRUE FALSE
# But if either vector contains a missing,
# the `==` operator returns an incorrect result:
a <- c( 1 , NA , 3 )
b <- c( 1 , NA , 4 )
# Here I'd want TRUE TRUE FALSE
a == b
# But I get TRUE NA FALSE
a <- c( 1 , NA , 3 )
b <- c( 1 , 2 , 4 )
# Here I'd want TRUE FALSE FALSE
a == b
# But I get TRUE NA FALSE again.
I get the result I want with:
mapply( `%in%` , a , b )
But mapply
seems heavy-handed to me.
Is there a more intuitive solution to this?
See Question&Answers more detail:os