[datatable-help] recode variable regardless of its order

Frank Erickson fperickson at wisc.edu
Thu Jun 16 15:49:32 CEST 2016


Another way of doing what Cedric did, if you're willing to overwrite the
original vars:

test[varA > varB, `:=`(VarA = VarB, VarB = VarA)]

On Thu, Jun 16, 2016 at 9:36 AM, Cedric Duprez <Cedric.Duprez at ign.fr> wrote:

> Hi,
>
> Perhaps something like this :
>
>  > test <- data.table(varA=c("a", "a", "a", "b", "b", "b", "c", "c",
> "c"), varB=c("a", "b", "c", "a", "b", "c", "a", "b", "c"))
>  > test
>     varA varB
> 1:    a    a
> 2:    a    b
> 3:    a    c
> 4:    b    a
> 5:    b    b
> 6:    b    c
> 7:    c    a
> 8:    c    b
> 9:    c    c
>  > test[, `:=`(varA2 = pmin(varA, varB), varB2 = pmax(varA, varB))]
>  > test
>     varA varB varA2 varB2
> 1:    a    a     a     a
> 2:    a    b     a     b
> 3:    a    c     a     c
> 4:    b    a     a     b
> 5:    b    b     b     b
> 6:    b    c     b     c
> 7:    c    a     a     c
> 8:    c    b     b     c
> 9:    c    c     c     c
>
> The two new variables (varA2 and varB2) look like what you're waiting
> for. Isn't it?
>
> Regards,
> Cedric
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20160616/737bd0d4/attachment.html>


More information about the datatable-help mailing list