a %in% b %in% c [1] FALSE...">

What does multiple% in% in a row do?

Consider:

a <- c("a", "b", "c")
b <- c("a", "e", "f")
c <- c("a", "h", "i")

> a %in% b %in% c
[1] FALSE FALSE FALSE

      

I would expect this to evaluate to TRUE FALSE FALSE

, since the first element in each vector is "a". Why is it wrong?

+3


source to share


1 answer


First of all, you perform this operation: a %in% b

as a result TRUE FALSE FALSE

. Then you are basically making a chain c(TRUE,FALSE,FALSE) %in% c

that will of course lead to everyone False

.

You can try this to get your desired vector of booleans (given position):

a == Reduce(function(u,v) ifelse(u==v, u, F), list(a,b,c))
#[1]  TRUE FALSE FALSE

      



If you want position-independent generic elements, you can do:

Reduce(intersect, list(a,b,c))

      

+5


source







All Articles