简体   繁体   中英

R: how to find common elements with the same indices in multiple vectors

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
  }
}

We can do this using a simple comparison check:

x == y

and subsetting x by it: x[x==y] . Then the question is how to best loop it over the combinations.

Here, I'll use outer to take the all by all output of each combination of the list of vectors, and call a Vectorized anonymous function on it.

v1 <- c(1, 99, 10, 11, 23)
v2 <- c(1, 99, 10, 23, 11)
v3 <- c(2, 4, 10, 13, 23)

l = list(v1,v2,v3)


outer(l,l,Vectorize(function(x,y){x[x==y]}))

     [,1]      [,2]      [,3]     
[1,] Numeric,5 Numeric,3 Numeric,2
[2,] Numeric,3 Numeric,5 10       
[3,] Numeric,2 10        Numeric,5

if you look in the output matrix, each cell is the overlap of the indexed lists:

output[1,2]
[[1]]
[1]  1 99 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM