How to find matches between two lists of names

I have two long name vectors (list.1, list.2). I want to run a loop to check if any name in list .2 matches any name in list .1. If so, I want to add to the vector result a value for the position of the matching name in the list of vectors .1.

for (i in list.2){ for (j in list.1){ if(length(grep(list.2[i], list.1[j]), ignore.case=TRUE)==0){ append(result, j) break } else append(nameComment.corresponding, 0) } } 

The above code is really brute force, and since my vectors are between 5,000 and 60,000 in length, it is likely to run over 360,000,000 cycles. How could I improve it?

+4
source share
1 answer

which and %in% are likely to be useful for this task or match depending on what you are going to do. It should be noted that match returns the index of the first match of the first argument in the second argument (that is, if you have multiple values ​​in the lookup table, only the first match will be returned):

 set.seed(123) # I am assuming these are the values you want to check if they are in the lookup 'table' list2 <- sample( letters[1:10] , 10 , repl = T ) [1] "c" "h" "e" "i" "j" "a" "f" "i" "f" "e" # I am assuming this is the lookup table list1 <- letters[1:3] [1] "a" "b" "c" # Find which position in the lookup table each value is, NA if no match match(list2 , list1 ) [1] 3 NA NA NA NA 1 NA NA NA NA 
+3
source

Source: https://habr.com/ru/post/1496288/


All Articles