title: Count number of factor levels that match a character string in R
body: <p>I have a factor with several levels, and I am trying to obtain the number of levels in which the factor levels contains a given string.</p>
  
  <p>Given this factor:</p>
  
  <pre><code>exdata &lt;- factor(c("Test1","Test2","Sample1","Sample2","Test1","Test2","Sample3"))
  </code></pre>
  
  <p>I want to find number of levels in exdata containing "Sample" or "Test."</p>
  
  <p>My solution thus far has been to use <code>nlevels</code>, <code>droplevels</code>, and <code>grep</code>:</p>
  
  <pre><code>nlevels(droplevels(exdata[grep("Test",exdata)]))
  # Correct/intended answer is 2
  nlevels(droplevels(exdata[grep("Sample",exdata)]))
  # Correct/intended answer is 3
  </code></pre>
  
  <p>Is there a more concise way to do this?</p>