<div dir="ltr"><div>Hi,</div><div><br></div><div>I really like the DT1[DT2,z:=...] idiom. Unfortunately, the value of a merge() on other columns is a new data.table, so modifying DT1, like merge(DT1,DT2,by=...)[,z:=...], is not possible. Or is there actually a way to do this that I am missing?</div>

<div><br></div><div>If this syntax -- DT1[DT2,z:=...,by=c(key(DT1),x)] -- behaved differently, allowing the "by" to determine which columns were merged on, that would solve my issue, I guess. </div><div><br></div>

<div>By the way, when you use by=c(key(DT),x), you get a speedup from DT's being keyed, right?</div><div><br></div><div><b>Some background</b> (rest of email): I've coded a value function iteration rather inefficiently and am looking into a few different directions for improving it. Efficiency matters because the result will enter a likelihood I need to maximize. (Value function iteration is solving a dynamic programming problem with discrete periods and a finite horizon from the horizon/final period backwards.)</div>

<div><br></div><div>I was alternating between two data tables and doing things like DT1[DT2[y==t],z:=...] and DT2[DT1[y==t-1],q:=...], changing the keys on both tables before each merge-assign. If <a href="http://r-forge.r-project.org/tracker/index.php?func=detail&aid=1007&group_id=240&atid=978">secondary keys were implemented</a>, I'd have just gone with that. (<a href="http://lists.r-forge.r-project.org/pipermail/datatable-help/2010-May/000028.html">Tom's secondary key method</a> mentioned in the last link only works for subsetting, not merge-assigning, as far as I can tell.)</div>

<div><br></div><div>I think I'm getting a big slow-down because I'm rekeying four times per iteration (two tables x two merge-assigns) and because I'm rekeying the entire table, when I'm only assigning to a subset. That second problem is easier to fix, I guess.</div>

<div><br></div><div>Now I am considering making a single DT with key=intersect(key(DT1),key(DT2)) and using that instead, if I can figure out a way to do what I need to with it.</div><div><br></div><div>Thanks,</div><div>

<br></div><div>Frank</div></div>