rdata.table

Is there some way to keep variable names from.SD+.SDcols together with non .SD variable names in data.table?


Given a data.table

library(data.table)
DT = data.table(x=rep(c("b","a","c"),each=3), v=c(1,1,1,2,2,1,1,2,2), y=c(1,3,6), a=1:9, b=9:1)
DT
   x v y a b
1: b 1 1 1 9
2: b 1 3 2 8
3: b 1 6 3 7
4: a 2 1 4 6
5: a 2 3 5 5
6: a 1 6 6 4
7: c 1 1 7 3
8: c 2 3 8 2
9: c 2 6 9 1

if one does

DT[, .(a, .SD), .SDcols=x:y]
   a .SD.x .SD.v .SD.y
1: 1     b     1     1
2: 2     b     1     3
3: 3     b     1     6
4: 4     a     2     1
5: 5     a     2     3
6: 6     a     1     6
7: 7     c     1     1
8: 8     c     2     3
9: 9     c     2     6

the variables from .SDcols become prefixed by .SD. On the other hand, if one tries, as in https://stackoverflow.com/a/62282856/997979,

DT[, c(.(a), .SD), .SDcols=x:y]
   V1 x v y
1:  1 b 1 1
2:  2 b 1 3
3:  3 b 1 6
4:  4 a 2 1
5:  5 a 2 3
6:  6 a 1 6
7:  7 c 1 1
8:  8 c 2 3
9:  9 c 2 6

the other variable name (a) become lost. (It is due to this reason that I re-ask the question which I initially marked as a duplicate to that linked above).

Is there some way to keep the names from both .SD variables and non .SD variables?

The goal is simultaneously being able to use .() to select variables without quotes and being able to select variables through .SDcols = patterns("...")

Thanks in advance!


Solution

  • not really sure why.. but it works ;-)

    DT[, .(a, (.SD)), .SDcols=x:y]
    #    a x v y
    # 1: 1 b 1 1
    # 2: 2 b 1 3
    # 3: 3 b 1 6
    # 4: 4 a 2 1
    # 5: 5 a 2 3
    # 6: 6 a 1 6
    # 7: 7 c 1 1
    # 8: 8 c 2 3
    # 9: 9 c 2 6