filter_all {dplyr} | R Documentation

## Filter within a selection of variables

### Description

These scoped filtering verbs apply a predicate expression to a selection of variables. The predicate expression should be quoted with `all_vars()` or `any_vars()` and should mention the pronoun `.` to refer to variables.

### Usage

    filter_all(.tbl, .vars_predicate, .preserve = FALSE)

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

    filter_at(.tbl, .vars, .vars_predicate, .preserve = FALSE)

### Arguments

`.tbl` | A `tbl` object.
`.vars_predicate` | A quoted predicate expression as returned by `all_vars()` or `any_vars()`. Can also be a function or purrr-like formula. In this case, the intersection of the results is taken by default and there's currently no way to request the union.
`.preserve` | when `TRUE` (the default), the grouping structure is preserved, otherwise it is recalculated based on the resulting data.
`.predicate` | A predicate function to be applied to the columns or a logical vector. The variables for which `.predicate` is or returns `TRUE` are selected. This argument is passed to `rlang::as_function()` and thus supports quosure-style lambda functions and strings representing function names.
`.vars` | A list of columns generated by `vars()`, a character vector of column names, a numeric vector of column positions, or `NULL`.

### Grouping variables

The grouping variables that are part of the selection are taken into account to determine filtered rows.

### Examples

    # While filter() accepts expressions with specific variables, the
    # scoped filter verbs take an expression with the pronoun `.` and
    # replicate it over all variables. This expression should be quoted
    # with all_vars() or any_vars():
    all_vars(is.na(.))
    any_vars(is.na(.))

    # You can take the intersection of the replicated expressions:
    filter_all(mtcars, all_vars(. > 150))

    # Or the union:
    filter_all(mtcars, any_vars(. > 150))

    # You can vary the selection of columns on which to apply the
    # predicate. filter_at() takes a vars() specification:
    filter_at(mtcars, vars(starts_with("d")), any_vars((. %% 2) == 0))

    # And filter_if() selects variables with a predicate function:
    filter_if(mtcars, ~ all(floor(.) == .), all_vars(. != 0))

    # We're working on a new syntax to allow functions instead,
    # including purrr-like lambda functions. This is already
    # operational, but there's currently no way to specify the union of
    # the predicate results:
    mtcars %>% filter_at(vars(hp, vs), ~ . %% 2 == 0)

[Package dplyr version 0.8.0.1 Index]