Forgot to change the second line. Here it goes dt <- data.table(a = runif(10)) ee <- quote(list(b = 3 * a, c =a +2)) dt[, names(ee)[-1] := eval(ee)]
On Fri Jan 16 2015 at 1:35:09 PM Frank Erickson <[email protected]> wrote: > Ah, good idea. That's what I'll do next time. > > I hadn't realized that "names" could look inside a quoted/language object. > > Thanks! > > On Fri, Jan 16, 2015 at 9:56 AM, Juan Manuel Truppia <[email protected]> > wrote: > >> With a little more work you could keep the definitions with the names. >> Here is how >> >> dt <- data.table(a = runif(10)) >> ee <- quote(list(3 * a, a +2)) >> dt[, names(ee)[-1] := eval(ee)] >> >> Sorry, hope this now sticks to the thread >> >> On Fri Jan 16 2015 at 11:19:57 AM Frank Erickson <[email protected]> >> wrote: >> >>> 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. >>> >>> 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. >>> >>> On Thu, Jan 15, 2015 at 11:07 AM, Juan Manuel Truppia < >>> [email protected]> wrote: >>> >>>> 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 >>>> >>>> dt <- data.table(a = runif(10)) >>>> ee <- quote(list(3 * a, a +2)) >>>> dt[, c("b", "c") := eval(ee)] >>>> >>>> I still don't know how to define the column names in the quoted >>>> expression, instead of in the `:=` call. >>>> >>>> Hope it helps >>>> >>>> >>>> On Thu Jan 15 2015 at 8:00:10 AM < >>>> [email protected]> wrote: >>>> >>>>> Send datatable-help mailing list submissions to >>>>> [email protected] >>>>> >>>>> To subscribe or unsubscribe via the World Wide Web, visit >>>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/ >>>>> listinfo/datatable-help >>>>> >>>>> or, via email, send a message with subject or body 'help' to >>>>> [email protected] >>>>> >>>>> You can reach the person managing the list at >>>>> [email protected] >>>>> >>>>> When replying, please edit your Subject line so it is more specific >>>>> than "Re: Contents of datatable-help digest..." >>>>> >>>>> >>>>> Today's Topics: >>>>> >>>>> 1. best way of eval-ing a list of quoted expressions (Frank >>>>> Erickson) >>>>> >>>>> >>>>> ---------------------------------------------------------------------- >>>>> >>>>> Message: 1 >>>>> Date: Wed, 14 Jan 2015 17:06:36 -0500 >>>>> From: Frank Erickson <[email protected]> >>>>> To: "data.table help" <[email protected]> >>>>> Subject: [datatable-help] best way of eval-ing a list of quoted >>>>> expressions >>>>> Message-ID: >>>>> <CAJd-hdmsub_h_oh2rGrBuvbGFCk4S9ZoTMi+ >>>>> [email protected]> >>>>> Content-Type: text/plain; charset="utf-8" >>>>> >>>>> Hi, >>>>> >>>>> I'm wondering what the most idiomatic or efficient approach is...? >>>>> Here's >>>>> my example: >>>>> >>>>> expr_nonlin = list( >>>>> early = quote(tt/TT*(tt/TT < .2)), >>>>> late = quote(tt/TT*(tt/TT > .8)) >>>>> ) >>>>> >>>>> # eval on a single expr works >>>>> data.table(tt=1,TT=100)[,early:=eval(expr_nonlin$early)][] >>>>> >>>>> # lapply eval does not work >>>>> data.table(tt=1,TT=100)[,names(expr_nonlin):=lapply( >>>>> expr_nonlin,eval)][] >>>>> >>>>> # (1) envir fixes it >>>>> DT <- data.table(tt=1,TT=100) >>>>> DT[,names(expr_nonlin):=lapply(expr_nonlin,eval,envir=DT)][] >>>>> >>>>> # (2) or a for loop >>>>> DT <- data.table(tt=1,TT=100) >>>>> for (i in names(expr_nonlin)) DT[,(i):=eval(expr_nonlin[[i]])] >>>>> >>>>> (1) and (2) both work. Is either preferable? >>>>> >>>>> (1) calls [.data.table fewer times, but messes around with >>>>> environments, >>>>> which always seem fragile. >>>>> >>>>> ------------------ >>>>> >>>>> One more quick question: In approach (1), is there a way to skip the >>>>> names(zzz):= part? I see that this doesn't work: >>>>> >>>>> DT <- data.table(tt=1,TT=100) >>>>> DT[,do.call(`:=`,lapply(expr_nonlin,eval,envir=DT))][] >>>>> >>>>> >>>>> Thanks, >>>>> >>>>> Frank >>>>> -------------- next part -------------- >>>>> An HTML attachment was scrubbed... >>>>> URL: <http://lists.r-forge.r-project.org/pipermail/ >>>>> datatable-help/attachments/20150114/035b1682/attachment-0001.html> >>>>> >>>>> ------------------------------ >>>>> >>>>> _______________________________________________ >>>>> datatable-help mailing list >>>>> [email protected] >>>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/ >>>>> listinfo/datatable-help >>>>> >>>>> End of datatable-help Digest, Vol 59, Issue 2 >>>>> ********************************************* >>>>> >>>> >>>> _______________________________________________ >>>> datatable-help mailing list >>>> [email protected] >>>> >>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help >>>> >>> >>> >
_______________________________________________ datatable-help mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
