1

how to filter for a string across columns one at a time

In the table below, I am trying to filter out rows with the word blue. The final result should be two rows for 11th and 12th oct. What I have tried which is not working:

tibble::tribble(
         ~date, ~col_1, ~col_2, ~col_3,
  "11/10/2021", "blue",  "jhf",  "fff",
  "12/10/2021",  "ert", "blue",  "kkk",
  "13/10/2021",  "yui",  "yui", "blkeee"
  ) %>% 
  
  mutate(date = lubridate::dmy(date)) %>% 
  
  dplyr::filter(across(.cols = where(is.character), 
                .fns = ~str_detect(., regex("blue", ignore_case = TRUE))
  ))

Any ideas?

Submitted October 11th 2021 by Admin

Answers
0

You may use if_any -

library(dplyr)
library(stringr) df %>% filter(if_any(starts_with('col'), ~str_detect(., regex("blue", ignore_case = TRUE)))) # date col_1 col_2 col_3
# <chr> <chr> <chr> <chr>
#1 11/10/2021 blue jhf fff #2 12/10/2021 ert blue kkk 

Admin | 1 week ago



Relevant Questions