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
_______________________________________________ datatable-help mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
