<div dir="ltr">So, I recently did something like this:<div><br></div><div><div><font face="courier new, monospace">DT <- data.table(name=c('Guff','Aw'),id=101:102,id2=1:2,key='id')</font></div><div>

<font face="courier new, monospace">y   <- rbind(list('No','NON',0L),DT,list('Extra','XTR',3L))</font></div><div><font face="courier new, monospace">x   <- data.table(id=as.character(101:102),z=1:2,key='id')</font></div>

</div><div><br></div><div>Those rows I added on do not belong in the positions I pasted them into, so when I tried...</div><div><br></div><div><div><font face="courier new, monospace">options(datatable.verbose=TRUE)</font></div>

<div><font face="courier new, monospace">x[y,newcol:=name]</font></div></div><div><br></div><div>...it failed, silently.</div><div><br></div><div>I'm guessing it saw the invalid key column in y and then proceeded to merge by y's column order instead. Because "name" comes before "id" (the column I thought was my key), no matches are found and newcol is not created. This is very, very confusing to see. Even with verbose on, I see no mention of "assigned to zero rows of x" or "matched on zero groups in y".</div>

<div><br></div><div>I've got several problems with how this worked:</div><div><br></div><div>(1) y should not inherit DT's key when I rbind it, or I should get a warning when rbinding a keyed data.table suggesting a better approach (that I clearly do not know about yet...?).</div>

<div><br></div><div>(2) I really don't like the silent failure to assign to or create newcol. Warnings are nice.</div><div><br></div><div>(3) It failed because DT1 had an invalid key (i.e., a "sorted" attribute on which it is not actually sorted). When I merge DT2[DT1] and it is found that DT1's key is invalid, I'd like to see (3a) a warning and (3b) it tell me explicitly that its merging on column order instead.</div>

<div><br></div><div>Note that there's a nice warning message when I reset the key:</div><div><br></div><div><div><font face="courier new, monospace">setkey(y,id)</font></div><div><font face="courier new, monospace"># Warning message:</font></div>

<div><font face="courier new, monospace"># In setkeyv(x, cols, verbose = verbose) :</font></div><div><font face="courier new, monospace">#   Already keyed by this key but had invalid row order, key rebuilt. If you didn't go under the hood please let datatable-help know so the root cause can be fixed.</font></div>

</div><div><br></div><div>What do you all think? Also, is there a right or safe way to do rbinding?</div><div><br></div><div>Thanks,</div><div><br></div><div>Frank</div></div>