title: How do I reorder data.table columns?
body: <p>How do I permute columns in a <code>data.table</code>?
  I can do that for a <code>data.frame</code>, but <code>data.table</code> overrides the method:</p>
  
  <pre><code>&gt; df &lt;- data.frame(a=1:3,b=4:6)
  &gt; df
    a b
  1 1 4
  2 2 5
  3 3 6
  &gt; df[c("b","a")]
    b a
  1 4 1
  2 5 2
  3 6 3
  &gt; dt &lt;- as.data.table(df)
  &gt; dt
     a b
  1: 1 4
  2: 2 5
  3: 3 6
  &gt; dt[c("b","a")]
  Error in `[.data.table`(dt, c("b", "a")) : 
    When i is a data.table (or character vector), x must be keyed (i.e. sorted, and, marked as sorted) so data.table knows which columns to join to and take advantage of x being sorted. Call setkey(x,...) first, see ?setkey.
  Calls: [ -&gt; [.data.table
  </code></pre>
  
  <p>Note that this is <em>not</em> a dupe for <a href="https://stackoverflow.com/questions/5620885/how-does-one-reorder-columns-in-r">How does one reorder columns in R?</a>.</p>