scoped: Operate on a selection of variables

scopedR Documentation

Operate on a selection of variables

Description

[Superseded]

Scoped verbs (⁠_if⁠, ⁠_at⁠, ⁠_all⁠) have been superseded by the use of pick() or across() in an existing verb. See vignette("colwise") for details.

The variants suffixed with ⁠_if⁠, ⁠_at⁠ or ⁠_all⁠ apply an expression (sometimes several) to all variables within a specified subset. This subset can contain all variables (⁠_all⁠ variants), a vars() selection (⁠_at⁠ variants), or variables selected with a predicate (⁠_if⁠ variants).

The verbs with scoped variants are:

  • mutate(), transmute() and summarise(). See summarise_all().

  • filter(). See filter_all().

  • group_by(). See group_by_all().

  • rename() and select(). See select_all().

  • arrange(). See arrange_all()

There are three kinds of scoped variants. They differ in the scope of the variable selection on which operations are applied:

  • Verbs suffixed with ⁠_all()⁠ apply an operation on all variables.

  • Verbs suffixed with ⁠_at()⁠ apply an operation on a subset of variables specified with the quoting function vars(). This quoting function accepts tidyselect::vars_select() helpers like starts_with(). Instead of a vars() selection, you can also supply an integerish vector of column positions or a character vector of column names.

  • Verbs suffixed with ⁠_if()⁠ apply an operation on the subset of variables for which a predicate function returns TRUE. Instead of a predicate function, you can also supply a logical vector.

Arguments

.tbl

A tbl object.

.funs

A function fun, a quosure style lambda ~ fun(.) or a list of either form.

.vars

A list of columns generated by vars(), a character vector of column names, a numeric vector of column positions, or NULL.

.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.

...

Additional arguments for the function calls in .funs. These are evaluated only once, with tidy dots support.

Grouping variables

Most of these operations also apply on the grouping variables when they are part of the selection. This includes:

  • arrange_all(), arrange_at(), and arrange_if()

  • distinct_all(), distinct_at(), and distinct_if()

  • filter_all(), filter_at(), and filter_if()

  • group_by_all(), group_by_at(), and group_by_if()

  • select_all(), select_at(), and select_if()

This is not the case for summarising and mutating variants where operations are not applied on grouping variables. The behaviour depends on whether the selection is implicit (all and if selections) or explicit (at selections). Grouping variables covered by explicit selections (with summarise_at(), mutate_at(), and transmute_at()) are always an error. For implicit selections, the grouping variables are always ignored. In this case, the level of verbosity depends on the kind of operation:

  • Summarising operations (summarise_all() and summarise_if()) ignore grouping variables silently because it is obvious that operations are not applied on grouping variables.

  • On the other hand it isn't as obvious in the case of mutating operations (mutate_all(), mutate_if(), transmute_all(), and transmute_if()). For this reason, they issue a message indicating which grouping variables are ignored.


tidyverse/dplyr documentation built on Nov. 4, 2024, 2:44 a.m.