title: data.table way of doing this dcast
body: <p>I have a data.table which looks like this:</p>
  
  <pre><code>tbl
            lon lat hour ens       date value
         1: 254  31   12   0 1994010100     0
         2: 254  31   12   0 1994010200     0
         3: 254  31   12   0 1994010300     0
         4: 254  31   12   0 1994010400     0
         5: 254  31   12   0 1994010500     0
        ---                                  
  40494956: 269  39   24  10 2007122700   200
  40494957: 269  39   24  10 2007122800   130
  40494958: 269  39   24  10 2007122900   240
  40494959: 269  39   24  10 2007123000   230
  40494960: 269  39   24  10 2007123100   150
  </code></pre>
  
  <p>and another one that looks like:</p>
  
  <pre><code>locs
      lon lat
   1: 260  33
   2: 261  33
   3: 262  33
   4: 263  33
   5: 260  34
   6: 261  34
  </code></pre>
  
  <p>And I am currently doing this with dcast to get it in the shape I need:</p>
  
  <pre><code>temp &lt;- dcast(tbl[locs], date ~ lon + lat + hour, fun.aggregate=mean, value.var="value")
  </code></pre>
  
  <p>This does exactly what I want (even the column names!) but is very slow.  I would like a data.table way of doing it but after reading a few threads here, I still can't quite work it out.  My latest attempt is this:</p>
  
  <pre><code>temp &lt;- tbl[locs, list(mean = mean(value), sd = sd(value)), by = list(date, lon, lat, hour)]
  </code></pre>
  
  <p>to collapse the <code>ens</code> variable (which works fine and NOTE that I am also calculating the sd as well), but I cannot reshape it.  My reshape attempt is:</p>
  
  <pre><code>temp[, as.list(setattr(list(mean,sd), 'names', list(lon, lat, hour))), by=list(date)]
  Error in setattr(list(mean, sd), "names", list(lon, lat, hour)) : 
    'names' attribute [3] must be the same length as the vector [2]
  </code></pre>
  
  <p>Any help is of course greatly appreciated.  Thank you.</p>