R filter_if() Function from dplyr

The dplyr::filter_if() function in R is used to apply a filtering condition to a subset of columns in a data frame or tibble. The subset of columns is dynamically determined based on a specified predicate.

This function allows you to filter rows in a data frame or tibble based on a condition applied to columns that meet a certain criterion. The criterion is defined by a predicate function that evaluates each column and returns TRUE or FALSE.


filter_if(.tbl, .predicate, .vars_predicate, .preserve = FALSE)


Name Description
.tbl It is a data frame or tibble.
.predicate It is a predicate function to be applied to the columns.
.vars_predicate A quoted predicate expression, typically wrapped in all_vars() or any_vars(), is applied to the selected columns.
.preserve It suggests whether to keep the original grouping structure or recalculate it. Defaults to FALSE.

Return value

It returns a filtered data frame or tibble based on the condition applied to the selected columns.

Example: Filtering numeric columns based on a condition

Basic working of R dplyr filter_if() function



df <- data.frame(
  a = c(1, 2, 3),
  b = c("A", "B", "C"),
  c = c(7, 8, 9)

df %>% filter_if(is.numeric, all_vars(. > 2))


  a  b  c
1 3  C  9

That’s all!

Leave a Comment