Remove all-FALSE columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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