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

Matthew Dowle mdowle at mdowle.plus.com
Thu Feb 21 23:06:02 CET 2013


Great.
I agree. It's sometimes difficult/time consuming (for me) to translate
rough ideas into precise changes that can be committed. You are probably
in the best position to guide the documentation as you would like. Anyone
is welcome to join the project and improve documentation or code. And
there is the data.table wiki.
Even more precise suggestions are very welcome.
Matthew

> Matthew,
>
> Thank you, this definitely does the trick.
>
> 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.
>
> 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.
>
> Thanks for these examples, they're quite elucidating.
>
>
> On Thu, Feb 21, 2013 at 10:34 AM, Matthew Dowle
> <mdowle at mdowle.plus.com>wrote:
>
>> **
>>
>>
>>
>> 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
>> 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
>> Thank you,
>> Gene
>>
>>
>>
>>
>




More information about the datatable-help mailing list