I have a dataframe, df. Some of its columns include logicals. I would like to drop the ones that are all FALSE.
library(tibble)
df <- tibble(A = rep(TRUE, 5),
B = rep(FALSE, 5),
C = c(TRUE, FALSE, TRUE, TRUE, FALSE))
df
# A tibble: 5 x 3
A B C
<lgl> <lgl> <lgl>
1 TRUE FALSE TRUE
2 TRUE FALSE FALSE
3 TRUE FALSE TRUE
4 TRUE FALSE TRUE
5 TRUE FALSE FALSE
The desired output is:
A C
<lgl> <lgl>
1 TRUE TRUE
2 TRUE FALSE
3 TRUE TRUE
4 TRUE TRUE
5 TRUE FALSE
I have tried selecting constant columns using the janitor package, but that will remove columns that are all TRUE also.
How may I do this? (I prefer a tidyverse solution, but barring that base R or some other available package is acceptable.)
Edit: My minimal working example above was too minimal. I should have mentioned that there are non-logical columns I want to keep too. The solution for me, provided by akrun in chat, was:
library(dplyr)
library(purrr)
df %>% select(where(~ is.logical(.) && any(.)), where(negate(is.logical)))