Home > Software design >  R: how to check if a vector is found in another vector of different length without using %in%
R: how to check if a vector is found in another vector of different length without using %in%

Time:10-17

vector_1 = c(4,3,5,1,2)
vector_2 = c(3,1)

output:
[1] FALSE  TRUE FALSE  TRUE FALSE

how do I get the output just by using basic operators/loops without using the operator %in% or any functions in R?

CodePudding user response:

One way with sapply() -

sapply(vector_1, function(x) any(x == vector_2))
[1] FALSE  TRUE FALSE  TRUE FALSE

CodePudding user response:

See match.fun(`%in%`)

match(vector_1,vector_2, nomatch = 0) > 0
  •  Tags:  
  • r
  • Related