Idiom for dropping a single column in a data.table
If you are wanting to remove the column permanently use := NULL
dat[, z := NULL]
If you have your columns to drop as a character string use ()
to force evaluation as a character string, not as the character name.
toDrop <- c('z')
dat[, (toDrop) := NULL]
If you want to limit the availability of the columns in .SD
, you can pass the .SDcols
argument
dat[,lapply(.SD, somefunction) , .SDcols = setdiff(names(dat),'z')]
However, data.table
inspects the j
arguments and only gets the columns you use any way. See FAQ 1.12
When you write X[Y,sum(foo*bar)], data.table automatically inspects the j expression to see which columns it uses.
and doesn't try and load all the data for .SD
(unless you have .SD
within your call to j
)
subset.data.table
is processing the call and eventually evaluating dat[, c('x','y'), with=FALSE]
using := NULL
should be basically instantaneous, howveer t does permanently delete the column.