Skip to content

Instantly share code, notes, and snippets.

@alexperrone
Created November 11, 2016 18:14
Show Gist options
  • Save alexperrone/217b11a5d59a1a8fe03334828c482479 to your computer and use it in GitHub Desktop.
Save alexperrone/217b11a5d59a1a8fe03334828c482479 to your computer and use it in GitHub Desktop.
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