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

Gene Leynes gleynes+r at gmail.com
Thu Feb 21 19:11:49 CET 2013


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
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20130221/abf9d3ca/attachment-0001.html>


More information about the datatable-help mailing list