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?
CodePudding user response:
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
In base R -
subset(df, Reduce(`|`, lapply(df[-1], grepl, pattern = "blue", ignore.case = TRUE)))
CodePudding user response:
# Base R option 1:
df[rowSums(Vectorize(`==`)("blue", df)) >= 1,]
# Base R option 2:
df[
apply(
df[,
vapply(
df,
is.character,
logical(1)
)
],
1,
function(x){
any(x == "blue")
}
),
]