title: Returning the matched string from a grepl match of multiple strings, rather than the logical
body: <p>Currently I'm using nested ifelse functions with grepl to check for matches to a vector of strings in a data frame, for example:</p>
  
  <pre><code># vector of possible words to match
  x &lt;- c("Action", "Adventure", "Animation")
  
  # data
  my_text &lt;- c("This one has Animation.", "This has none.", "Here is Adventure.")
  my_text &lt;- as.data.frame(my_text)
  
  my_text$new_column &lt;- ifelse (
    grepl("Action", my_text$my_text) == TRUE,
    "Action",
    ifelse (
      grepl("Adventure", my_text$my_text) == TRUE,
      "Adventure",
      ifelse (
        grepl("Animation", my_text$my_text) == TRUE,
        "Animation", NA)))
  
  &gt; my_text$new_column
  [1] "Animation" NA          "Adventure"
  </code></pre>
  
  <p>This is fine for just a few elements (e.g., the three here), but how do I return when the possible matches are much larger (e.g., 150)? Nested ifelse seems crazy. I know I can grepl multiple things at once as in the code below, but this return a logical telling me only if the string was matched, not which one was matched. I'd like to know what was matched (in the case of multiple, any of the matches is fine.</p>
  
  <pre><code>x &lt;- c("Action", "Adventure", "Animation")
  my_text &lt;- c("This one has Animation.", "This has none.", "Here is Adventure.")
  grepl(paste(x, collapse = "|"), my_text)
  
  returns: [1]  TRUE FALSE  TRUE
  what i'd like it to return: "Animation" ""(or FALSE) "Adventure"
  </code></pre>