If you put your vectors in a list, they'll be substantially easier to work with:
# make sample data
set.seed(47)
x <- replicate(6, rpois(3, 10), simplify = FALSE)
str(x)
# List of 6
# $ : int [1:3] 16 12 10
# $ : int [1:3] 9 10 6
# $ : int [1:3] 10 14 4
# $ : int [1:3] 7 6 4
# $ : int [1:3] 12 8 7
# $ : int [1:3] 7 11 8
Now iterate with lapply
:
lapply(x, function(y){sapply(x, function(z){y %in% z})})
## [[1]]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] TRUE FALSE FALSE FALSE FALSE FALSE
## [2,] TRUE FALSE FALSE FALSE TRUE FALSE
## [3,] TRUE TRUE TRUE FALSE FALSE FALSE
##
## [[2]]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] FALSE TRUE FALSE FALSE FALSE FALSE
## [2,] TRUE TRUE TRUE FALSE FALSE FALSE
## [3,] FALSE TRUE FALSE TRUE FALSE FALSE
## ... ... ... ... ... ... ...
which is a matrix for each vector, where the rows are the elements of that respective vector and the columns are each of the vectors in the list, and the values indicate whether that element is in that vector. Obviously each will match with itself, so the first column of the first element is all TRUE
, as is the second column of the second element, etc. Other TRUE
s indicate cross-vector matches. If lengths are inconsistent, it will return a nested list of the same information instead of a matrix. If you'd rather have a nested list anyway, change sapply
to lapply
.
Alternately, if you just want a vector of matches for each vector,
str(lapply(x, function(y){which(sapply(x, function(z){any(y %in% z)}))}))
## List of 6
## $ : int [1:4] 1 2 3 5
## $ : int [1:4] 1 2 3 4
## $ : int [1:4] 1 2 3 4
## $ : int [1:5] 2 3 4 5 6
## $ : int [1:4] 1 4 5 6
## $ : int [1:3] 4 5 6
where each element still contains itself as a match. Take out which
for Booleans instead of indices.