[Solved] subsetting columns in a datatable [duplicate]


We need to use with = FALSE

dt[, 1:2, with = FALSE]

This is explained in the ?data.table

with: By default with=TRUE and j is evaluated within the frame of x;
column names can be used as variables.

When with=FALSE j is a character vector of column names, a numeric
vector of column positions to select or of the form startcol:endcol,
and the value returned is always a data.table. with=FALSE is often
useful in data.table to select columns dynamically

solved subsetting columns in a datatable [duplicate]