An example data.table:
DT = data.table(
ID = c("b","b","b","a","a","c"),
a = 1:6,
b = 7:12,
c = 13:18
)
keep_cols <- c("a", "c")
All the following commands
DT[,.(a, c)]
DT[,c("a", "c")]
DT[,c(2,4)]
DT[, .SD, .SDcols = c(2,4)]
DT[, .SD, .SDcols = keep_cols]
DT[,..keep_cols]
result in
a c
<int> <int>
1: 1 13
2: 2 14
3: 3 15
4: 4 16
5: 5 17
6: 6 18