I want to filter with a tibble ignoring empty values.
Example, when the variables tibble have not empty values the filter works fine:
library(tidyverse)
library(datasets)
variables <- tibble(manufacturer = 'audi',
model = 'a4',
cyl = '6'
)
filtered <- mpg %>%
filter(manufacturer == variables$manufacturer,
model == variables$model,
cyl == variables$cyl)
# A tibble: 3 x 11
manufacturer model displ year cyl trans drv cty hwy fl
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr>
1 audi a4 2.8 1999 6 auto(l5) f 16 26 p
2 audi a4 2.8 1999 6 manual(~ f 18 26 p
3 audi a4 3.1 2008 6 auto(av) f 18 27 p
# ... with 1 more variable: class <chr>
When I put an '' (or NA) value on variables tibble, dplyr finds no matches and outputs a tibble with 0 obs.
# A tibble: 0 x 11
# ... with 11 variables: manufacturer <chr>, model <chr>, displ <dbl>,
# year <int>, cyl <int>, trans <chr>, drv <chr>, cty <int>,
# hwy <int>, fl <chr>, class <chr>
I want dplyr to ignore the empty values of variables and filter the others, is there any way to do it?
Greetings!