title: R: Check if strings in a vector are present in other vectors, and return name of the match
body: <p>I need a tool more selective than <code>%in%</code> or <code>match()</code>. I need a code that matches a vector of string with another vector, and that returns the names of the matches.</p>
  
  <p>Currently I have the following, </p>
  
  <pre><code>test &lt;- c("country_A", "country_B", "country_C", "country_D", "country_E", "country_F") rating_3 &lt;-  c("country_B", "country_D", "country_G", "country_K")
  rating_3 &lt;-  c("country_B", "country_D", "country_G", "country_K")
  rating_4 &lt;- c("country_C", "country_E", "country_M", "country_F)
  
  
  i &lt;- 1
  while (i &lt;= 33) {
    print(i)
    print(test[[i]])
    if (grepl(test[[i]], rating_3) == TRUE) {
      print(grepl(test[[i]], rating_3)) }
    i &lt;- i+1
  },
  </code></pre>
  
  <p>This should check each element of <code>test</code> present in <code>rating_3</code>, but for some reason, it returns only the position, the name of the string, and a warning; </p>
  
  <pre><code> [1]
   [country_A]
   There were 6 warnings (use warnings() to see them)
  </code></pre>
  
  <p>I need to know what this piece of code fails, but I'd like to eventually have it return the name only when it's inside another vector, and if possible, testing it against several vectors at once, having it print the name of the vector in which it fits, something like </p>
  
  <pre><code>[1]
  [String]
  [rating_3]
  </code></pre>
  
  <p>How could I get something like that? </p>