Drops columns that have only one value in a data.table.

drop_constant_cols(DT, copy = FALSE)

Arguments

DT

A data.table.

copy

(logical, default: FALSE) Whether the data.table should be copied before any columns are dropped. If FALSE, the default, columns are dropped from DT by reference.

Details

If DT is a data.frame that is not a data.table, constant columns are still dropped, but since DT will be copied, copy should be set to TRUE to avoid a warning. If DT is a data.frame and all but one of the columns are constant, a data.frame will still be returned, as opposed to the values of the sole remaining column, which is the default behaviour of base data.frame.

If all columns are constant, drop_constant_cols returns a Null data table if DT is a data.table, but a data frame with 0 columns and nrow(DT) otherwise.

Examples

library(data.table) X <- data.table(x = c(1, 1), y = c(1, 2)) drop_constant_cols(X)
#> y #> 1: 1 #> 2: 2