title: Selecting columns by regex created from different frame
body: <p>I want to select by regex a number of columns from a data frame. The problem is, I want to use pairs of words stored in another frame to do so. To be more specific, column names of mycounts data frame:</p>
  
  <pre><code>aa1 aa2 aa3 bb1 bb2 bb3 ff1 ff2 ff3 cc1 cc2 cc3 dd1 dd2 dd3
  </code></pre>
  
  <p>and I want to extract to separate frames following pairs (mypairs):</p>
  
  <pre><code>aa bb
  aa cc
  bb cc
  cc dd
  dd ff
  </code></pre>
  
  <p>where aa and bb denote starting patter for columns to select from mycounts. </p>
  
  <p>I can get stuff manually putting desired patterns like this: </p>
  
  <pre><code>tmpdf &lt;- subset(mycounts.test, select=(names(mycounts)[grep('^aa|^bb',names(mycounts))]))
  </code></pre>
  
  <p>do something with tmpdf
  select next pair from mypairs
  extract columns from mycounts
  do again the same thing with tmpdf etc.</p>
  
  <p>but it will be good to always have pattern of output columns enforced by mypairs. With "|" in grep instead of getting dd then ff columns I have it ff then dd. So I am doing:</p>
  
  <pre><code>tmpdf1 &lt;- subset(mycounts.test, select=(names(mycounts)[grep('^dd',names(mycounts))]))
  tmpdf2 &lt;- subset(mycounts.test, select=(names(mycounts)[grep('^ff',names(mycounts))]))
  tmpdf &lt;- cbind(tmpdf1, tmpdf2))
  </code></pre>
  
  <p>Most importantly, naive constructing regex from just first column of mypairs does not work:</p>
  
  <pre><code>for (i in 1:nrow(mypairs)) { foo &lt;- paste('^', mypairs[i,1], sep=""); print(grep(foo, mycounts)) }
  </code></pre>
  
  <p>This where I am stuck. Many thanks for your help.</p>