<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html><body>
<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>
<pre> </pre>
<pre>Matthew</pre>
<pre> </pre>
<pre> </pre>
<pre> </pre>
<p>On 21.02.2013 15:49, Gene Leynes wrote:</p>
<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%"><!-- html ignored --><!-- head ignored --><!-- meta ignored -->
<div style="font-family: arial,sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;">
<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-family: arial,sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;">My example:   <a style="color: #1155cc;" href="https://gist.github.com/geneorama/4998308">https://gist.github.com/geneorama/4998308</a></div>
<div style="font-family: arial,sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;">I was about to post a question, but SO suggested this answer:</div>
<div style="font-family: arial,sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"><a style="color: #1155cc;" href="http://stackoverflow.com/questions/8374816/loop-through-columns-in-a-data-table-and-transform-those-columns">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>
</body></html>