[datatable-help] recode variable regardless of its order

Cedric Duprez Cedric.Duprez at ign.fr
Thu Jun 16 15:36:47 CEST 2016


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


More information about the datatable-help mailing list