[datatable-help] Integration with magrittr

Michael Smith my.r.help at gmail.com
Wed Jul 1 03:32:51 CEST 2015


Steve,

You're absolutely right that DT[, a := a * 2] is easier to read, but 
that's not my point.

Maybe I have not pointed this out clearly enough and I'm sorry about 
that, but my point is NOT how to multiply a number by two.

Instead, my point is how to do some calculation with a variable and then 
reassign that result back to that variable. (That's the whole rationale 
behind `%<>%` in magrittr!)

This calculation could IN REALITY be of course more complicated than *2, 
and could involve many functions chained together and a long variable 
name that is cumbersome to read/type repeatedly, e.g.

DT[, long.variable.name := long.variable.name %>% function.a %>% function.b]

I do think that the other version would be more easy to read and type:

DT[, long.variable.name %<>% function.a %>% function.b]

Hope that clarifies.

M

On 07/01/2015 04:06 AM, Steve Lianoglou wrote:
> Hi,
>
>> I love data.table and I also love magrittr.
>
> I really love both of these, too.
>
>> However, some things don't work,
>> e.g.
>>
>> DT <- data.table(a = 1:3, b = 4:6)
>> DT[, a %<>% `*`(2)]    # instead of DT[, a := a %>% `*`(2)]
>
> In my personal opinion, though, I feel like this might be going a
> stretch too far.
>
> Although this might just be a contrived example, it's hard for me to
> divine an instance where this would look any less contrived. I mean,
> is:
>
> DT[, a %<>% `*`(2)] ## or
> DT[, a := a %>% `*`(2)]
>
> Really any more useful/simpler than:
>
> DT[, a := a * 2]
>
> Can't think of when I'd prefer the magrittr'ized version ... perhaps
> when the column name (`a`) is actually a much longer? (so that %<>%
> saves you some typing), or?
>
> -steve
>


More information about the datatable-help mailing list