title: best way to transpose data.table
body: <p>[UPDATE: there is now a native <code>transpose()</code> function in <code>data.table</code> package]</p>
  
  <p>I often need to transpose a <code>data.table</code>, every time it takes several lines of code and I am wondering if there's any better solution than mine. </p>
  
  <p>if we take sample table</p>
  
  <pre><code>library(data.table)
  mydata &lt;- data.table(col0=c("row1","row2","row3"),
                       col1=c(11,21,31),
                       col2=c(12,22,32),
                       col3=c(13,23,33))
  
  mydata
  # col0 col1 col2 col3
  # row1   11   12   13
  # row2   21   22   23
  # row3   31   32   33
  </code></pre>
  
  <p>and just transpose it with <code>t()</code>, it will be transposed to the matrix with conversion to <code>character</code> type, while applying <code>data.table</code> to such matrix will lose <code>row.names</code>:</p>
  
  <pre><code>t(mydata)
  # [,1]   [,2]   [,3]  
  # col0 "row1" "row2" "row3"
  # col1 "11"   "21"   "31"  
  # col2 "12"   "22"   "32"  
  # col3 "13"   "23"   "33"  
  
  data.table(t(mydata))
  #   V1   V2   V3
  # row1 row2 row3
  #   11   21   31
  #   12   22   32
  #   13   23   33
  </code></pre>
  
  <p>so I had to write a function for this:</p>
  
  <pre><code>tdt &lt;- function(inpdt){
    transposed &lt;- t(inpdt[,-1,with=F]);
    colnames(transposed) &lt;- inpdt[[1]];
    transposed &lt;- data.table(transposed, keep.rownames=T);
    setnames(transposed, 1, names(inpdt)[1]);
    return(transposed);
  }
  
   tdt(mydata)
  # col0 row1 row2 row3
  # col1   11   21   31
  # col2   12   22   32
  # col3   13   23   33
  </code></pre>
  
  <p>is there anything I could optimize here or do it in "nicer" way?</p>