title: grep pattern for column names
body: <p>I have a data frame and want to grep column names that fit particular patterns. I have four sets of patterns:</p>
  
  <pre><code># set 1 (underscore, no A)
    cat1_1
    cat12_12
  
  # set 2 (underscore, A)
    cat4_4A
    cat18_18A
  
  # set 3 (no underscore, no p)
    dog2
    dog12
  
  # set 4 (no underscore, p)
    dog2p
    dog12p
  </code></pre>
  
  <p>My actual data frame contains different numbers of columns per set, but I am showing just two columns per set in this example for simplicity.</p>
  
  <pre><code>ex &lt;- data.frame(cat1_1=c("1a", "1a"),
                   cat12_12=c("1b", "1b"),
                   cat4_4A=c("2a", "2a"),
                   cat18_18A=c("2b", "2b"),
                   dog2=c("3a", "3a"),
                   dog12=c("3b", "3b"),
                   dog2p=c("4a", "4a"),
                   dog12p=c("4b", "4c"))
  ex
  #      cat1_1 cat12_12 cat4_4A cat18_18A dog2 dog12 dog2p dog12p
  #1     1a       1b      2a        2b   3a    3b    4a     4b
  #2     1a       1b      2a        2b   3a    3b    4a     4c
  </code></pre>
  
  <p>I want to grep <code>names(ex)</code> so that I grab all set 1 variables, then separately, all set 2 variables, and so on. So for instance, <code>grep(PATTERN, names(ex))</code> for set 1 should return:</p>
  
  <p><code>[1] "cat1_1"   "cat12_12"</code></p>
  
  <p>I'd appreciate help with the grep pattern for each set. One constraint is that I do not want to change any column names.</p>