title: R: %in% Operator with Wildcard/REGEX
body: <p>I have a data frame with a concatenated string, the last 11 digits of which are a census tract. I have a separate list of strings, in which the last 2 or 5 digits represent states or counties, respectively. I've concatenated a <code>*</code> on the end of the 2 or 5 digit id. I need to go through the data frame and flag whether the <code>trans</code> variable (census tract) is in the <code>patterns</code> vector (state or county), allowing the <code>*</code> to represent the remaining 9 or 6 digits in <code>trans</code>. </p>
  
  <p>As shown in the code below, I've gotten this to work by collapsing all the <code>pattern</code>s into a single string with <code>collapse="|"</code>, and <code>grepl</code>ing the two. However, I'm wondering if I can accomplish this through a vector operation because 1) it feels like I should be able to, and 2) in practice, the list of patterns is enormous and it feels foolish to put them into a single character variable. </p>
  
  <p>Is there anything similar to the <code>%in%</code> operator, but with regex/wildcard character support?</p>
  
  <pre><code>library(dplyr)
  
  trans &lt;- c("1-IA-45045000100",
             "2-IA-23003001801",
             "3-LITP-01001000100",
             "4-OTP-06006000606",
             "4-OTP-06010001001",
             "1-IA-45001010002",
             "2-IA-45045000101",
             "2-LITP-23005005002")
  df &lt;- data.frame(id = 1:8, trans)
  
  patterns &lt;- c("1-IA-45*",
                "2-LITP-23005*",
                "4-OTP-06*")
  
  # This works, but I'm looking for a better way
  patterns_string &lt;- paste(patterns, collapse="|")
  df &lt;- df %&gt;% mutate(match = ifelse(grepl(patterns_string, df$trans), TRUE, FALSE))
  
  # Is there anyway to keep the patterns in a vector and check for whether they
  # any of them grepl with each row or my data frame or to use %in% with a 
  # wildcard character?
  
  # "argument 'pattern' has length &gt; 1 and only first element will be used" 
  df &lt;- df %&gt;% mutate(match = ifelse(grepl(patterns, df$trans), TRUE, FALSE))
  
  # Can't take advantage of the 'wild character '*'
  df &lt;- df %&gt;% mutate(match = trans %in% patterns)
  </code></pre>