drop_constant_cols: Drop constant columns

View source: R/drop_constant_cols.R

drop_constant_colsR Documentation

Drop constant columns

Description

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

Usage

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)




HughParsonage/hutils documentation built on Feb. 12, 2023, 8:26 a.m.