title: How to subset a dataframe using multiple regular expressions in variable names?
body: <p>I have a dataframe with over 500 named variables, and I want to select only the columns whose names include the strings "xyz" and "abc". The first letter is sometimes capitalized, sometimes not, so I'm using a regular expression "[Aa]bc" etc. </p>
  
  <p>I have the full dataset in a dataframe called df, and I'm building a new dataframe called df2 by selecting variables out of df using grep(). I can do it one at a time and stick them together with cbind(), but I'd like to know how to do it all in one go. </p>
  
  <p>I thought I could pass multiple conditions to grep(), but seem to be getting stuck here. </p>
  
  <p>With a really simplified example:</p>
  
  <pre><code>df &lt;- data.frame(abc=1:3, def=4:6, Xyz=7:9, Abc=10:12, xyz=13:15)
  
    abc def Xyz Abc xyz
  1   1   4   7  10  13
  2   2   5   8  11  14
  3   3   6   9  12  15
  </code></pre>
  
  <p>I successfully got the columns I needed using two separate lines:</p>
  
  <pre><code>df2 &lt;- df[,grep("[Aa]bc", names(df), value=TRUE)]
  df3 &lt;- df[,grep("[Xx]yz", names(df), value=TRUE)]
  df4 &lt;- cbind(df2, df3)
  </code></pre>
  
  <p>When I try to do all this at once using:</p>
  
  <pre><code>df2 &lt;- df[,grep("[Aa]bc" | "[Xx]yz", names(df), value=TRUE)]
  </code></pre>
  
  <p>I got the following error: </p>
  
  <blockquote>
    <p>Error in "[Aa]bc" | "[Xx]yz" :    operations are possible only for
    numeric, logical or complex types</p>
  </blockquote>
  
  <p>I also tried passing the conditions to grep as a list of the strings, but it didn't work:</p>
  
  <pre><code>df2 &lt;- df[,grep(c("[Aa]bc", "[Xx]yz"), names(df), value=TRUE)]
  </code></pre>
  
  <p>It only used the first list item with a warning: </p>
  
  <blockquote>
    <p>In grep(c("[Aa]bc", "[Xx]yz"), names(df), value = TRUE) :   argument
    'pattern' has length > 1 and only the first element will be used</p>
  </blockquote>
  
  <p>so it only selected the columns with "[Aa]bc", and skipped "[Xx]yz". </p>
  
  <p>Is there an easier way to do this?</p>