Home > OS >  Return TRUE/FALSE if common elements/no common elements between vectors
Return TRUE/FALSE if common elements/no common elements between vectors

Time:05-10

I'm looking for an efficient way to create a boolean vector which returns TRUE if one or more of a number of specified variables e.g. c(1,2,3) are in another vector e.g. c(4,5,6,1).

In this example the output sought would be TRUE as the element 1 is present in both vectors.

As far as I know %in% only permits checking one variable at a time and using the | operator is inefficient in this case given the number of potential variables I need to check for. Using intersect() returns logical(0) rather than FALSE, and sum(c(1,2,3) == c(4,5,6,1)) > 1 returns FALSE as the common elements are not in the same position.

CodePudding user response:

You can use any:

vec <- c(1,2,3)
vec2 <- c(4,5,6,1)

any(vec %in% vec2)
#[1] TRUE

CodePudding user response:

Another option:

vector1 <- c(1,2,3)
vector2 <- c(4,5,6,1)

any(Reduce(intersect, list(vector1, vector2)))

Output:

[1] TRUE

CodePudding user response:

Here's another option using is.element with any:

vec1 <- c(1,2,3)
vec2 <- c(4,5,6,1)

any(is.element(vec1, vec2))

# [1] TRUE

Or another option is to use match with any (though would probably be less efficient):

any(match(vec1, vec2), na.rm = TRUE)

CodePudding user response:

Using any is the most efficient way, but just to offer another solution, here's one using sum:

!!sum(c(1,2,3) %in% c(4,5,6,1))

 # [1] TRUE

Or we can use length:

length(base::intersect(c(1,2,3), c(4,5,6,1))) != 0

CodePudding user response:

One more not great option:

anyDuplicated( c(unique(vec), unique(vec2)) ) > 0
# [1] TRUE

CodePudding user response:

One more with negating all:

v1 <- c(1,2,3)
v2 <- c(4,5,6,1)

!all(v1 %in% v2)
[1] TRUE
  • Related