title: selecting columns in R in certain order
body: <p>I have a dataset with a large number of columns (+300). I am interested only in a few of them. I selected these in this way:</p>
  
  <pre><code>ks2reduced &lt;- ks2data[,ks2meta[,2]]
  </code></pre>
  
  <p>where ks2meta[,2] contains the names of the columns I am interested in. This works but the problem is that the resulting data frame has the columns in a different order than the order set in ks2meta[,2]: </p>
  
  <pre><code>&gt; ks2meta[,2]
  [1] RECTYPE   LEA   ESTAB  URN SCHNAME            
  [6] ..
  &gt; colnames(ks2reduced)
  [1] "TAB1618"    "LEA"        "ALPHAIND"   "TKS1APS"    "TPUPYEAR"   "PBELIG"     "URN"        
  [6] ..
  </code></pre>
  
  <p>I am, actually, surprised by this behaviour of R - to me it seems to be a "language gotcha" (behaviour that is probably explained but not expected). How can I select the columns in the same order as they are listed in a vector?</p>