My question is somewhat similar to https://stackoverflow.com/questions/3695677/how-to-find-common-elements-from-multiple-vectors question. Suppose I have 3 vectors all of the same length:
v1 <- c(1, 99, 10, 11, 23)
v2 <- c(1, 99, 10, 23, 11)
v3 <- c(2, 4, 10, 13, 23)
Comparing v1 and v2:
It's easy to see that v1
and v2
have the same elements, which would be returned by Reduce(intersect, list(v1, v2))
. However, I only want the overlap to be returned ONLY if their order in the vectors are the same as well. Therefore, I would only want to see 1 99 10
because they're both ordered as indices 1, 2, 3, respectively.
Comparing v1 and v3:
Here, 10 23
should be returned.
Comparing v2 and v3:
Only 10
should be returned.
In order to handle pairwise comparisons, I think I'll use a nested loop:
v_all = list(v1, v2, v3)
length_v = length(v_all) - 1
for(i in 1:length_v){
v_ind = 1:length(v_all)
v_2 = v_ind[-which(v_all == i)]
for(j in v_2){
#code for finding overlap
}
}