<div><br></div><div>Matthew,</div><div><br></div><div>Thank you, this definitely does the trick.</div><div><br></div><div>I would suggest that `get` and `set` deserve more prominent coverage in the guides. I've been using data.table for some time and I didn't know about either. I've seen `get`, but I didn't realize that you could use it that way.</div>
<div><br></div><div>I tried for some time to use the .SD trick, but I couldn't get it to work. The .SD function is still a little mysterious to me, although I've used it a couple of times in different situations.</div>
<div><br></div><div>Thanks for these examples, they're quite elucidating.</div><div><br></div><br><div class="gmail_quote">On Thu, Feb 21, 2013 at 10:34 AM, Matthew 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"><u></u>
<div>
<p> </p>
<p>Hi,</p>
<pre>for (.col in FactorColumns) dt[,.col:=as.factor(get(.col)),with=FALSE]</pre>
<pre>for (.col in NumericColumns) dt[,.col:=as.numeric(get(.col)),with=FALSE]</pre>
<pre> </pre>
<pre>or,</pre>
<pre> </pre>
<pre>for (.col in FactorColumns) dt[,c(.col):=as.factor(get(.col))]<br>for (.col in NumericColumns) dt[,c(.col):=as.numeric(get(.col))]</pre>
<pre> </pre>
<pre>or,</pre>
<pre> </pre>
<pre>for (.col in FactorColumns) set(dt,j=.col,value=as.factor(dt[[.col]])<br>for (.col in NumericColumns) set(dt,j=.col,value=as.numeric(dt[[.col]])</pre>
<pre> </pre>
<pre>or (with no for loop),</pre>
<pre> </pre>
<pre>dt[, c(FactorColumns):=lapply(.SD,as.factor), .SDcols=FactorColumns]<br>dt[, c(NumericColumns):=lapply(.SD,as.numeric), .SDcols=NumericColumns]</pre>
<pre> </pre>
<pre> </pre>
<pre>But the for loops are probably faster and easier to follow.<br><br></pre>
<pre>That S.O. is quite old and could do with updating. := and with=FALSE have improved since then.</pre><span class="HOEnZb"><font color="#888888">
<pre> </pre>
<pre>Matthew</pre></font></span><div><div class="h5">
<pre> </pre>
<pre> </pre>
<pre> </pre>
<p>On <a href="tel:21.02.2013%2015" value="+12102201315" target="_blank">21.02.2013 15</a>:49, Gene Leynes wrote:</p>
<blockquote type="cite" style="padding-left:5px;border-left:#1010ff 2px solid;margin-left:5px;width:100%">
<div style="font-size:12.800000190734863px;font-family:arial,sans-serif">
<div>I want to update a group of columns programmatically. Based on a predetermined list I want to convert the classes of some columns.</div>
<div>This is simple a simple task with <span style="font-family:'courier new',monospace">data.frame</span>, but in data.table this requires a confusing combination of `<span style="font-family:'courier new',monospace">substitute</span>`, `<span style="font-family:'courier new',monospace">as.symbol</span>`, and `<span style="font-family:'courier new',monospace">eval</span>`.</div>
<div>Am I doing this right?</div>
</div>
<div style="font-size:12.800000190734863px;font-family:arial,sans-serif">My example: <a style="color:#1155cc" href="https://gist.github.com/geneorama/4998308" target="_blank">https://gist.github.com/geneorama/4998308</a></div>
<div style="font-size:12.800000190734863px;font-family:arial,sans-serif">I was about to post a question, but SO suggested this answer:</div>
<div style="font-size:12.800000190734863px;font-family:arial,sans-serif"><a style="color:#1155cc" href="http://stackoverflow.com/questions/8374816/loop-through-columns-in-a-data-table-and-transform-those-columns" target="_blank">http://stackoverflow.com/questions/8374816/loop-through-columns-in-a-data-table-and-transform-those-columns</a><br>
<div>Thank you,</div>
<div>Gene<br><br></div>
</div>
</blockquote>
<p> </p>
<div> </div>
</div></div></div>
</blockquote></div><br>