Must have latest version of dplyr.

library(tidyverse)

Import

data <- read_csv("data/data.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────────────────────────────
## cols(
##   id = col_double(),
##   color_a = col_character(),
##   color_b = col_character(),
##   color_c = col_character()
## )
data

Variables

color_values <- c("blue", "brown")
my_color_cols <- c("color_a", "color_b", "color_c")

Filter with across

Filter across to exclude rows

This is the old filter_at version that excludes columns where target colors exist.

data %>% 
  filter_at(
    vars(all_of(my_color_cols)),
    all_vars(
      !(. %in% color_values)
    )
  )

This is the across() version of this:

data %>% 
  filter(
    across(all_of(my_color_cols),
    ~ !(.x %in% color_values))
  )

Filter across columns to keep rows

This used if_any to find rows where the color_values exist in any my_color_cols. across() doesn’t work well with filter. Read this.

data %>% 
  filter(
    if_any(my_color_cols, ~ .x %in% color_values)
  )
## Note: Using an external vector in selections is ambiguous.
## ℹ Use `all_of(my_color_cols)` instead of `my_color_cols` to silence this message.
## ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This message is displayed once per session.

Removing columns with NA

This excludes rows when is.na is in any column. I think I need all_of() only if using filter? Not quite same reference.

I’m not quite sure why this doesn’t work with if_any(my_color_cols) like above.

data %>% 
 filter(across(all_of(my_color_cols), ~ !is.na(.x)))

When I first did it as filter(across(my_color_cols, ~ !is.na(.x))) I got this message:

Note: Using an external vector in selections is ambiguous. ℹ Use all_of(my_color_cols) instead of my_color_cols to silence this message. ℹ See https://tidyselect.r-lib.org/reference/faq-external-vector.html.