Skip to content
Prev 6089 / 29559 Next

Problem with %IN% ==FALSE when data frames are equal

On Mon, 13 Jul 2009, Jim Burke wrote:

            
Exactly. Just calculate the condition vector first, then condition with 
for example if(any()), only subsetting with "[" if the condition is met. 
You'll have to decide what to do if it isn't.

Always try small examples interactively before writing complicated and (in 
this case) logically flawed scripts - you were assuming that at least one 
logical vector value would be true.

Note that %in% and match() do the same thing, so you can also do match() - 
check the ordering of arguments first! - and check for NAs in the returned 
index vector - or use which() on the condition, checking on the length of 
the output; this is what "[" does internally.

match(c("a", "c", "e"), letters)
match(c("A"), letters)
which(letters %in% c("a", "c", "e"))
which(letters %in% c("A"))
length(which(letters %in% c("A")))

Hope this helps,

Roger