<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
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">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<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>
<fieldset></fieldset>
<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>
</body>
</html>