<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 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 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 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 href="http://is.na" target="_blank">is.na</a>(foo),
foo:=FALSE]<br>
fbq.cp[<a href="http://is.na" target="_blank">is.na</a>(bar),
bar:=FALSE]<br>
fbq.cp[<a 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 href="http://is.na" target="_blank">is.na</a>(x), eval(x):=FALSE]<br>
for (x in c("foo", "bar", "qux")) fbq[<a 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 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>
<fieldset></fieldset>
<br>
</div></div><pre>_______________________________________________
datatable-help mailing list
<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.r-project.org</a>
<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></pre>
</blockquote>
<br>
</div>
</blockquote></div><br></div>