[datatable-help] Update columns in data.table programmatically

Matthew Dowle mdowle at mdowle.plus.com
Thu Feb 21 17:34:52 CET 2013


 

Hi, 

for (.col in FactorColumns)
dt[,.col:=as.factor(get(.col)),with=FALSE]

for (.col in NumericColumns)
dt[,.col:=as.numeric(get(.col)),with=FALSE]

or,

for (.col in
FactorColumns) dt[,c(.col):=as.factor(get(.col))]
for (.col in
NumericColumns) dt[,c(.col):=as.numeric(get(.col))]

or,

for (.col in
FactorColumns) set(dt,j=.col,value=as.factor(dt[[.col]])
for (.col in
NumericColumns) set(dt,j=.col,value=as.numeric(dt[[.col]])

or (with no
for loop),

dt[, c(FactorColumns):=lapply(.SD,as.factor),
.SDcols=FactorColumns]
dt[, c(NumericColumns):=lapply(.SD,as.numeric),
.SDcols=NumericColumns]

But the for loops are probably faster and
easier to follow.

That S.O. is quite old and could do with updating. :=
and with=FALSE have improved since then.

Matthew

On 21.02.2013 15:49,
Gene Leynes wrote: 

> I want to update a group of columns
programmatically. Based on a predetermined list I want to convert the
classes of some columns. 
> This is simple a simple task with
data.frame, but in data.table this requires a confusing combination of
`substitute`, `as.symbol`, and `eval`. 
> Am I doing this right? 
> My
example: https://gist.github.com/geneorama/4998308 [1] 
> I was about to
post a question, but SO suggested this answer: 
>
http://stackoverflow.com/questions/8374816/loop-through-columns-in-a-data-table-and-transform-those-columns
[2]
> 
> Thank you, 
> Gene

 

Links:
------
[1]
https://gist.github.com/geneorama/4998308
[2]
http://stackoverflow.com/questions/8374816/loop-through-columns-in-a-data-table-and-transform-those-columns
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20130221/93efc703/attachment.html>


More information about the datatable-help mailing list