Skip to content

Commit

Permalink
more about across(.fns = NULL) (#6027)
Browse files Browse the repository at this point in the history
* more about .fns = NULL (the default)

still need to find a decent example

* complete.cases(across()) combo to show that result of across() may be used as another function input.

* use `x_complete` instead of  `z`
  • Loading branch information
romainfrancois authored Oct 22, 2021
1 parent 74ace27 commit 434caa2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
17 changes: 15 additions & 2 deletions R/across.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
#' `across()` supersedes the family of "scoped variants" like
#' `summarise_at()`, `summarise_if()`, and `summarise_all()`.
#'
#' @param cols,.cols <[`tidy-select`][dplyr_tidy_select]> Columns to transform.
#' @param .cols,cols <[`tidy-select`][dplyr_tidy_select]> Columns to transform.
#' Because `across()` is used within functions like `summarise()` and
#' `mutate()`, you can't select or compute upon grouping variables.
#' @param .fns Functions to apply to each of the selected columns.
#' Possible values are:
#'
#' - `NULL`, to returns the columns untransformed.
#' - A function, e.g. `mean`.
#' - A purrr-style lambda, e.g. `~ mean(.x, na.rm = TRUE)`
#' - A list of functions/lambdas, e.g.
#' `list(mean = mean, n_miss = ~ sum(is.na(.x))`
#' - `NULL`: the default value, returns the selected columns in a data
#' frame without applying a transformation. This is useful for when you want to
#' use a function that takes a data frame.
#'
#' Within these functions you can use [cur_column()] and [cur_group()]
#' to access the current column and grouping keys respectively.
Expand Down Expand Up @@ -106,6 +108,17 @@
#' group_by(Species) %>%
#' summarise(across(starts_with("Sepal"), list(mean, sd), .names = "{.col}.fn{.fn}"))
#'
#' # across() returns a data frame, which can be used as input of another function
#' df <- data.frame(
#' x1 = c(1, 2, NA),
#' x2 = c(4, NA, 6),
#' y = c("a", "b", "c")
#' )
#' df %>%
#' mutate(x_complete = complete.cases(across(starts_with("x"))))
#' df %>%
#' filter(complete.cases(across(starts_with("x"))))
#'
#' # if_any() and if_all() ----------------------------------------------------
#' iris %>%
#' filter(if_any(ends_with("Width"), ~ . > 4))
Expand Down
23 changes: 18 additions & 5 deletions man/across.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 434caa2

Please sign in to comment.