mercredi 20 juin 2018

R - Comparing 2 lists and extracting similar entries

I am currently trying to compare 2 lists and pull out the entry in List2 that matches List1. Here is what I have thus far:

List1 <- list(1,2,3)
List2 <- list(2,4,6)
is.element(List1[2],List2)
for(i in 1:length(List1)){
  if(is.element(List1[i],List2)){
    print(List2)
  }else{

  }
}

At the moment when i=2 it just prints the whole of List2, which isn't what I want, can someone let me know how to extract specifically the element that matches List1[i]? If List1[i] doesn't match List2 I just want nothing to happen.

In the future I am planning to use this to compare a list of job titles and a list of sentences. If a job title comes up the entire sentence will be pulled out of the list of sentences. Any advice on this would be greatly appreciated

Aucun commentaire:

Enregistrer un commentaire