Home > Blockchain >  Ignore `NULL` parameters in dplyr::filter
Ignore `NULL` parameters in dplyr::filter

Time:09-29

Given:

  • gear <- NULL
  • carb <- NULL
  • mpg <- 20

How would I pass all of these variables to dplyr::filter but have it ignore the NULL arguments and not return an error? This becomes especially problematic when I have 10 variables in a function, any of which could be NULL, and need to filter a data frame by any possible permutation of the variables. I do not want my function to need a considerable number of if statements in order to run properly.

As expected, this fails but is in-line with what I want to do.

mtcars |>
  filter(gear == !!gear & carb == !!carb & mpg == !!mpg)

In practice, I want dplyr to basically evaluate: mtcars |> filter(mpg == !!mpg) because it is the only variable that is not missing.

CodePudding user response:

One way is to customize an operator similar to the operator == but returning TRUE when the second input is NULL.

library(dplyr)

`%==%` <- function (e1, e2) {
  if (is.null(e2)) {
    return(TRUE)
  } else {
    return(e1 == e2)
  }
}

gear <- NULL
carb <- NULL
mpg <- 21

mtcars |>
  filter(gear %==% !!gear & carb %==% !!carb & mpg %==% !!mpg)
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
  • Related