Skip to content

Instantly share code, notes, and snippets.

@alexperrone
Created November 11, 2016 18:14
Embed
What would you like to do?
Remove all-FALSE columns
# How to batch-remove columns which are all FALSE in data.table
library(data.table)
dt <- data.table(x = c(TRUE, FALSE, FALSE),
y = c(FALSE, FALSE, FALSE), # all FALSE
z = c(FALSE, FALSE, TRUE),
a = c(FALSE, FALSE, FALSE) # all FALSE
)
dt
all_false_cols <- apply(dt, MARGIN = 2, function(x){ all(x == FALSE)})
all_false_cols
dt2 <- dt[ , !all_false_cols, with = FALSE]
dt2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment