<div dir="ltr"><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
Copied from this SO post: <a href="http://stackoverflow.com/questions/23097461">http://stackoverflow.com/questions/23097461</a></p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
Here's some interesting behavior that I noticed with data.table 1.9.2</p><pre class="" style="margin-top:0px;margin-bottom:10px;padding:5px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;overflow:auto;width:auto;max-height:600px;word-wrap:normal;color:rgb(0,0,0);line-height:17.804800033569336px">
<code style="margin:0px;padding:0px;border:0px;vertical-align:baseline;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:inherit"><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">></span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">   testFun </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"><-</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(0,0,139)">function</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">val</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">)</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">{</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">
        </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(0,0,139)">if</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">val </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">==</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">'geteeee'</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">)</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(0,0,139)">return</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">data</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">.</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">table</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">x</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">=</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">4</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">,</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">y</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">=</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">3</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">))</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">
        </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(0,0,139)">if</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">val </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">==</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">'get'</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">)</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(0,0,139)">return</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">data</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">.</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">table</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">y</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">=</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">3</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">,</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">x</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">=</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">4</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">))</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">
    </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">}</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">
</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">></span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">   tbl </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">=</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> data</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">.</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">table</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">val</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">=</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">c</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">'geteeee'</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">,</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">'get'</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">))</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">
</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">></span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">   tbl</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">[,</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> testFun</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">val</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">),</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(0,0,139)">by</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">=</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">val</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">]</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">
       val x y
</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">1</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">:</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> geteeee </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">4</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">3</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">
</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">2</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">:</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">     </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(0,0,139)">get</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">3</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(128,0,0)">4</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">
</span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">></span><span class="" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span></code></pre>
<p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
When the column order of the data tables returned from each call to testFun are mixed (but have the same name and number of columns), data.table silently binds the tables together without taking into account that they are out of order. This was probably done for speed, but I found the behavior quite unexpected, and would have appreciated at least a warning.</p>
<p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
Is there a way that I can get data.table to warn or error when this situation happens?</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
This happened in my analysis code and caused values for two DVs to be intermixed. The reason why it happened is that in the 'testFun' there is a branch and the returned data table is created within both sides of the branch. The branch is necessary to handle the case where the data table used to create the final returned data table is empty. So on one side of that branch I basically create an empty data table with the correct columns, and on the other side the data table is created from the first. The point is that the column order for the data tables returned from each side of the branch are different. Now this is certainly a bug on my part in 'testFun'. However I could have caught the issue much earlier if I had received a warning from data.table when the by operation completed and the resulting tables were bound together. </p>
<p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
Also since there isn't a check for column order, it does make me worry that there are other places in my analysis code where the same thing could be happening. What would be ideal is if there was some way for me to tell if that is the case. Perhaps a warning, temporarily increasing a 'safety' level as an options call, etc. Usually data.table is great at warning me when things are not quite right, so I was surprised when I noticed the current behavior. I understand that this was done for speed. So maybe temporarily increasing a 'safety' level is a way to keep things fast by default and have additional checks (for a speed cost) when the user wants them? This sort of mimics how compiler optimization declarations are done in common lisp.</p>
<p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
-Clayton</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
<br></p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
<br></p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
<br></p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
<br></p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:13.63636302947998px;vertical-align:baseline;clear:both;color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:17.804800033569336px">
<br></p></div>