How do you remove columns from a data.frame?

I use data.table‘s := operator to delete columns instantly regardless of the size of the table.

DT[, coltodelete := NULL]

or

DT[, c("col1","col20") := NULL]

or

DT[, (125:135) := NULL]

or

DT[, (variableHoldingNamesOrNumbers) := NULL]

Any solution using <- or subset will copy the whole table. data.table‘s := operator merely modifies the internal vector of pointers to the columns, in place. That operation is therefore (almost) instant.

Leave a Comment