I'm trying to find a method of filtering over a specific range of columns in a data frame to filter to rows that contain all elements of a character vector somewhere within that range of columns.
For example, if my column range was the one below:
library(dplyr)
set.seed(10)
df <- tibble(
a = sample(LETTERS[1:10], 10),
b = sample(LETTERS[1:10], 10),
c = sample(LETTERS[1:10], 10),
d = sample(LETTERS[1:10], 10),
e = sample(LETTERS[1:10], 10)
)
df
#> # A tibble: 10 x 5
#> a b c d e
#> <chr> <chr> <chr> <chr> <chr>
#> 1 F G I F C
#> 2 C F F A J
#> 3 D A G B A
#> 4 E E C G F
#> 5 A C H C B
#> 6 B I D D H
#> 7 G H E E E
#> 8 J D A J G
#> 9 H J B H D
#> 10 I B J I I
and I wanted to filter to rows that had at least one instance of "A"
and at least one instance of "C"
I could use filter_at
twice to get the desired result:
df %>%
filter_at(vars(a:e), any_vars(. == "A")) %>%
filter_at(vars(a:e), any_vars(. == "C"))
#> # A tibble: 2 x 5
#> a b c d e
#> <chr> <chr> <chr> <chr> <chr>
#> 1 C F F A J
#> 2 A C H C B
However , I'm trying to implement this in shiny where filter criteria comes as a vector c("A", "C")
from a select input so is there a way I can do this with a single filter function using the vector?
Using %in%
won't work because it returns any rows with "A"
or "C"
df %>%
filter_at(vars(a:e), any_vars(. %in% c("A", "C")))
#> # A tibble: 6 x 5
#> a b c d e
#> <chr> <chr> <chr> <chr> <chr>
#> 1 F G I F C
#> 2 C F F A J
#> 3 D A G B A
#> 4 E E C G F
#> 5 A C H C B
#> 6 J D A J G
Thanks!