<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Melanie,</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">`set` modifies by reference. Yours'll make a copy. </div> <div id="bloop_sign_1392379623481940992" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">Arun</div></div> <div style="color:gray"><hr style="style:linear">From: <span style="color:black">Bacou, Melanie</span> <a href="mailto:mel@mbacou.com">Bacou, Melanie</a><br>Reply: <span style="color:black">Bacou, Melanie</span> <a href="mailto:mel@mbacou.com">mel@mbacou.com</a><br>Date: <span style="color:black">February 14, 2014 at 12:52:56 PM</span><br>To: <span style="color:black">Matt Dowle</span> <a href="mailto:mdowle@mdowle.plus.com">mdowle@mdowle.plus.com</a>, <span style="color:black">John Laing</span> <a href="mailto:john.laing@gmail.com">john.laing@gmail.com</a><br>Subject: <span style="color:black"> Re: [datatable-help] Force evaluation of first argument to [ <br></span></div> <blockquote type="cite" class="clean_bq"><span><div bgcolor="#FFFFFF" text="#000000"><div>
<title></title>
Hi John, Matt,<br>
<br>
In this case, why not simply using the standard data.table approach
with .SD?<br>
<br>
<tt>fbq.cp[, lapply(.SD, function(x) ifelse(is.na(x), FALSE, x)),
.SDcols=c("foo", "bar", "qux")]</tt><br>
<br>
--Mel.<br>
<br>
<br>
<div class="moz-cite-prefix">On 2/12/2014 2:22 PM, Matt Dowle
wrote:<br></div>
<blockquote cite="mid:52FBC9DC.2010809@mdowle.plus.com" type="cite">
<div class="moz-cite-prefix"><br>
Ha. Yes we certainly don't hold back from making the messages
as long and as helpful as possible. If the code knows, or can
know what exactly is wrong, it's a deliberate policy to put that
info right there into the message. data.table is written by users;
i.e. we wrote it for ourselves doing real jobs. I think that may be
the root of that. If any messages could more helpful,
those suggestions are very welcome.<br>
<br>
Matt<br>
<br>
On 12/02/14 17:58, John Laing wrote:<br></div>
<blockquote cite="mid:CAA3Wa=u2PwRW_Od4XTodtZ4uDSN7RZmOs33Uaaix8GN7OuRpag@mail.gmail.com" type="cite">
<div dir="ltr">Thanks, Matt! With a slight amendment that works
great:
<div>
<div>for (x in c("foo", "bar", "qux")) set(fbq,
which(<a moz-do-not-send="true" href="http://is.na">is.na</a>(fbq[[x]])), x, FALSE)</div>
</div>
<div><br></div>
<div>Which highlights an opportunity to say that I really
appreciate the unusually helpful error messages in this
package.</div>
<div><br></div>
<div>-John</div>
</div>
<div class="gmail_extra"><br>
<br>
<div class="gmail_quote">On Wed, Feb 12, 2014 at 12:44 PM, Matt
Dowle <span dir="ltr"><<a moz-do-not-send="true" href="mailto:mdowle@mdowle.plus.com" target="_blank">mdowle@mdowle.plus.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div><br>
Hi John,<br>
<br>
In examples like this I'd use set() and [[, since it's a bit
easier to write but memory efficient too.<br>
<br>
for (x in c("foo", "bar", "qux")) set(fbq,
<a moz-do-not-send="true" href="http://is.na" target="_blank">is.na</a>(fbq[[x]]), x,
FALSE)
[untested]<br>
<br>
A downside here is one repetition of the "fbq" symbol, but
can live with that. If you have a large number of
columns (and I've been surprised just how many columns some
poeple have!) then calling set() many times has lower overhead than
DT[, :=], see ?set. Note also that [[ is base R,
doesn't copy the column and often useful to use with
data.table.<br>
<br>
Or, use get() in either i or j rather than eval().<br>
<br>
HTH, Matt
<div>
<div class="h5"><br>
<br>
<br>
On 12/02/14 17:24, John Laing wrote:<br></div>
</div>
</div>
<blockquote type="cite">
<div>
<div class="h5">
<div dir="ltr">Let's say I merge together several data.tables such
that I wind up<br>
with lots of NAs:<br>
<br>
require(data.table)<br>
foo <- data.table(k=1:4, foo=TRUE, key="k")<br>
bar <- data.table(k=3:6, bar=TRUE, key="k")<br>
qux <- data.table(k=5:8, qux=TRUE, key="k")<br>
fbq <- merge(merge(foo, bar, all=TRUE), qux, all=TRUE)<br>
print(fbq)<br>
# k foo bar qux<br>
# 1: 1 TRUE NA NA<br>
# 2: 2 TRUE NA NA<br>
# 3: 3 TRUE TRUE NA<br>
# 4: 4 TRUE TRUE NA<br>
# 5: 5 NA TRUE TRUE<br>
# 6: 6 NA TRUE TRUE<br>
# 7: 7 NA NA TRUE<br>
# 8: 8 NA NA TRUE<br>
<br>
I want to go through those columns and turn each NA into FALSE. I
can<br>
do this by writing code for each column:<br>
<br>
fbq.cp <- copy(fbq)<br>
fbq.cp[<a moz-do-not-send="true" href="http://is.na" target="_blank">is.na</a>(foo), foo:=FALSE]<br>
fbq.cp[<a moz-do-not-send="true" href="http://is.na" target="_blank">is.na</a>(bar), bar:=FALSE]<br>
fbq.cp[<a moz-do-not-send="true" href="http://is.na" target="_blank">is.na</a>(qux), qux:=FALSE]<br>
print(fbq.cp)<br>
# k foo bar qux<br>
# 1: 1 TRUE FALSE FALSE<br>
# 2: 2 TRUE FALSE FALSE<br>
# 3: 3 TRUE TRUE FALSE<br>
# 4: 4 TRUE TRUE FALSE<br>
# 5: 5 FALSE TRUE TRUE<br>
# 6: 6 FALSE TRUE TRUE<br>
# 7: 7 FALSE FALSE TRUE<br>
# 8: 8 FALSE FALSE TRUE<br>
<br>
But I can't figure out how to do it in a loop. More precisely, I
can't<br>
figure out how to make the [ operator evaluate its first argument
in<br>
the context of the data.table. All of these have no effect:<br>
for (x in c("foo", "bar", "qux")) fbq[<a moz-do-not-send="true" href="http://is.na" target="_blank">is.na</a>(x),
eval(x):=FALSE]<br>
for (x in c("foo", "bar", "qux")) fbq[<a moz-do-not-send="true" href="http://is.na" target="_blank">is.na</a>(eval(x)),
eval(x):=FALSE]<br>
for (x in c("foo", "bar", "qux")) fbq[eval(<a moz-do-not-send="true" href="http://is.na" target="_blank">is.na</a>(x)),
eval(x):=FALSE]<br>
<br>
I'm running R 3.0.2 on Linux, data.table 1.8.10.<br>
<br>
Thanks in advance,<br>
John</div>
<br>
<br></div>
</div>
<pre>_______________________________________________
datatable-help mailing list
<a moz-do-not-send="true" href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.r-project.org</a>
<a moz-do-not-send="true" 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>
</pre></blockquote>
<br></div>
</blockquote>
</div>
<br></div>
</blockquote>
<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
datatable-help mailing list
<a class="moz-txt-link-abbreviated" href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a>
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a>
</pre></blockquote>
<br>
<pre class="moz-signature" cols="72">--
Melanie BACOU
International Food Policy Research Institute
Agricultural Economist, HarvestChoice
Work +1(202)862-5699
E-mail <a class="moz-txt-link-abbreviated" href="mailto:mel@mbacou.com">mel@mbacou.com</a>
Visit harvestchoice.org
</pre>
_______________________________________________
<br>datatable-help mailing list
<br>datatable-help@lists.r-forge.r-project.org
<br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</div></div></span></blockquote></body></html>