<div dir="ltr">Ah, good idea. That's what I'll do next time.<div><br></div><div>I hadn't realized that "names" could look inside a quoted/language object.</div><div><br></div><div>Thanks!</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 16, 2015 at 9:56 AM, Juan Manuel Truppia <span dir="ltr"><<a href="mailto:jmtruppia@gmail.com" target="_blank">jmtruppia@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">With a little more work you could keep the definitions with the names. Here is how<br><br><div style="line-height:19.7999992370605px">dt <- data.table(a = runif(10))</div><div style="line-height:19.7999992370605px">ee <- quote(list(3 * a, a +2))</div><div>dt[, names(ee)[-1] := eval(ee)]<br><br></div><div>Sorry, hope this now sticks to the thread</div><br><div class="gmail_quote">On Fri Jan 16 2015 at 11:19:57 AM Frank Erickson <<a href="mailto:fperickson@wisc.edu" target="_blank">fperickson@wisc.edu</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks! I keep forgetting that I can eval() a larger statement. I think I'll stick to an option that keeps the names close to the definitions for now, though.<div><br></div><div>By the way, you replied to the r-forge forum "digest," outside of the original thread. I might've missed the reply if there weren't so few active conversations.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 15, 2015 at 11:07 AM, Juan Manuel Truppia <span dir="ltr"><<a href="mailto:jmtruppia@gmail.com" target="_blank">jmtruppia@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I don't know what you are trying to achieve, but I usually quote the list, instead of generating a list of quotes. I think that your issue is similar to something I've faced in the past, and I usually solve it like this<div><br></div><div>dt <- data.table(a = runif(10))</div><div>ee <- quote(list(3 * a, a +2))</div><div>dt[, c("b", "c") := eval(ee)]<br><div><br></div><div>I still don't know how to define the column names in the quoted expression, instead of in the `:=` call.</div><div><br></div><div>Hope it helps</div><div><br></div></div><br><div class="gmail_quote">On Thu Jan 15 2015 at 8:00:10 AM <<a href="mailto:datatable-help-request@lists.r-forge.r-project.org" target="_blank">datatable-help-request@lists.r-forge.r-project.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Send datatable-help mailing list submissions to<br>
<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.<u></u>r-project.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-<u></u>project.org/cgi-bin/mailman/<u></u>listinfo/datatable-help</a><br>
<br>
or, via email, send a message with subject or body 'help' to<br>
<a href="mailto:datatable-help-request@lists.r-forge.r-project.org" target="_blank">datatable-help-request@lists.<u></u>r-forge.r-project.org</a><br>
<br>
You can reach the person managing the list at<br>
<a href="mailto:datatable-help-owner@lists.r-forge.r-project.org" target="_blank">datatable-help-owner@lists.r-<u></u>forge.r-project.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than "Re: Contents of datatable-help digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
1. best way of eval-ing a list of quoted expressions (Frank Erickson)<br>
<br>
<br>
------------------------------<u></u>------------------------------<u></u>----------<br>
<br>
Message: 1<br>
Date: Wed, 14 Jan 2015 17:06:36 -0500<br>
From: Frank Erickson <<a href="mailto:fperickson@wisc.edu" target="_blank">fperickson@wisc.edu</a>><br>
To: "data.table help" <<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.<u></u>r-project.org</a>><br>
Subject: [datatable-help] best way of eval-ing a list of quoted<br>
expressions<br>
Message-ID:<br>
<<a href="mailto:CAJd-hdmsub_h_oh2rGrBuvbGFCk4S9ZoTMi%2BFrfEssH9mqoLiA@mail.gmail.com" target="_blank">CAJd-hdmsub_h_<u></u>oh2rGrBuvbGFCk4S9ZoTMi+<u></u>FrfEssH9mqoLiA@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Hi,<br>
<br>
I'm wondering what the most idiomatic or efficient approach is...? Here's<br>
my example:<br>
<br>
expr_nonlin = list(<br>
early = quote(tt/TT*(tt/TT < .2)),<br>
late = quote(tt/TT*(tt/TT > .8))<br>
)<br>
<br>
# eval on a single expr works<br>
data.table(tt=1,TT=100)[,<u></u>early:=eval(expr_nonlin$early)<u></u>][]<br>
<br>
# lapply eval does not work<br>
data.table(tt=1,TT=100)[,<u></u>names(expr_nonlin):=lapply(<u></u>expr_nonlin,eval)][]<br>
<br>
# (1) envir fixes it<br>
DT <- data.table(tt=1,TT=100)<br>
DT[,names(expr_nonlin):=<u></u>lapply(expr_nonlin,eval,envir=<u></u>DT)][]<br>
<br>
# (2) or a for loop<br>
DT <- data.table(tt=1,TT=100)<br>
for (i in names(expr_nonlin)) DT[,(i):=eval(expr_nonlin[[i]]<u></u>)]<br>
<br>
(1) and (2) both work. Is either preferable?<br>
<br>
(1) calls [.data.table fewer times, but messes around with environments,<br>
which always seem fragile.<br>
<br>
------------------<br>
<br>
One more quick question: In approach (1), is there a way to skip the<br>
names(zzz):= part? I see that this doesn't work:<br>
<br>
DT <- data.table(tt=1,TT=100)<br>
DT[,do.call(`:=`,lapply(expr_<u></u>nonlin,eval,envir=DT))][]<br>
<br>
<br>
Thanks,<br>
<br>
Frank<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20150114/035b1682/attachment-0001.html" target="_blank">http://lists.r-forge.r-<u></u>project.org/pipermail/<u></u>datatable-help/attachments/<u></u>20150114/035b1682/attachment-<u></u>0001.html</a>><br>
<br>
------------------------------<br>
<br>
______________________________<u></u>_________________<br>
datatable-help mailing list<br>
<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.<u></u>r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-<u></u>project.org/cgi-bin/mailman/<u></u>listinfo/datatable-help</a><br>
<br>
End of datatable-help Digest, Vol 59, Issue 2<br>
******************************<u></u>***************<br>
</blockquote></div>
<br>_______________________________________________<br>
datatable-help mailing list<br>
<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br></blockquote></div><br></div>
</blockquote></div>
</blockquote></div><br></div>