<div dir="ltr">Hi,<div><br></div><div>I'm wondering what the most idiomatic or efficient approach is...? Here's my example:</div><div><br></div><div><div><font face="monospace, monospace">expr_nonlin<span style="white-space:pre"> </span>=<span style="white-space:pre"> </span>list(</font></div><div><font face="monospace, monospace"><span style="white-space:pre">  </span>early = quote(tt/TT*(tt/TT < .2)),</font></div><div><font face="monospace, monospace"><span style="white-space:pre">  </span>late  = quote(tt/TT*(tt/TT > .8))</font></div><div><font face="monospace, monospace">)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"># eval on a single expr works</font></div><div><font face="monospace, monospace">data.table(tt=1,TT=100)[,early:=eval(expr_nonlin$early)][]</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"># lapply eval does not work</font></div><div><font face="monospace, monospace">data.table(tt=1,TT=100)[,names(expr_nonlin):=lapply(expr_nonlin,eval)][]</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"># (1) envir fixes it</font></div><div><font face="monospace, monospace">DT <- data.table(tt=1,TT=100)</font></div><div><font face="monospace, monospace">DT[,names(expr_nonlin):=lapply(expr_nonlin,eval,envir=DT)][]</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"># (2) or a for loop</font></div><div><font face="monospace, monospace">DT <- data.table(tt=1,TT=100)</font></div><div><font face="monospace, monospace">for (i in names(expr_nonlin)) DT[,(i):=eval(expr_nonlin[[i]])]</font></div></div><div><br></div><div>(1) and (2) both work. Is either preferable?</div><div><br></div><div>(1) calls [.data.table fewer times, but messes around with environments, which always seem fragile.</div><div><br></div><div>------------------</div><div><br></div><div>One more quick question: In approach (1), is there a way to skip the <font face="monospace, monospace">names(zzz):=</font> part? I see that this doesn't work:</div><div><br></div><div><div><font face="monospace, monospace">DT <- data.table(tt=1,TT=100)</font></div><div><font face="monospace, monospace">DT[,do.call(`:=`,lapply(expr_nonlin,eval,envir=DT))][]</font></div></div><div><br></div><div><br></div><div>Thanks,</div><div><br></div><div>Frank</div></div>