I want to filter my data if all of the values in a subset of columns are NA
.
I found an answer here that works brilliantly for all columns, but in this case I want to exclude "wrapper" columns from the filter operation.
library(dplyr)
df <- tibble(a = letters[1:3], b = c(NA, NA, 3), c = c(NA, 2, 3), d = letters[1:3])
# works, but I've lost my surrounding columns
df %>%
select(-one_of(c("a", "d"))) %>%
filter_all(any_vars(!is.na(.)))
# only works if all columns are all not NA (or vice versa), I've lost my partially NA rows
df %>%
filter(across(-one_of(c("a", "d")),
~ !is.na(.)))
Desired outcome:
> df[2:3,]
# A tibble: 2 x 4
a b c d
<chr> <dbl> <dbl> <chr>
1 b NA 2 b
2 c 3 3 c