pairwise: Apply a function (or functions) across all combinations of...

View source: R/pairwise.R

pairwiseR Documentation

Apply a function (or functions) across all combinations of pairs of selected columns

Description

pairwise() creates all combinations of columns and then applies function(s) to these.

pairwise() largely mirror dplyr::across() in style (and is meant to be used primarily within dplyr::mutate() and dplyr::summarise()).

Usage

pairwise(
  .cols = everything(),
  .fns = NULL,
  ...,
  .names = NULL,
  .is_commutative = FALSE
)

Arguments

.cols

<tidy-select> Columns to transform.

.fns

Functions to apply to each pair of the selected columns. Possible values are:

  • A function

  • A purrr-style lambda, e.g. ~ stats::cor.test(.x, .y)$p.value

  • A list of functions / lambdas, e.g.

  list(difference = `-`, ratio = ~ .x / .y)

The output length of a function should in most cases be 1 (in the summarisng case) or the length of an individual input (in the mutating case), similar to what is expected by summarise() and mutate() respectively.

...

Additional arguments for the function calls in .fns.

.names

A glue specification that describes how to name the outputted columns. Use {.col_x} and {.col_y} to specify inputted column names and {.fn} to specify function name when .fns is a named list.

Default format when .fns is not a named list is: "{.col_x}_{.col_y}_{.fn}"

When .fns is a named list default format is: "{.col_x}_{.col_y}_{.fn}"

.is_commutative

If TRUE, only create new column for {.col_x}_{.col_y} (not {.col_y}_{.col_x}). Use to save computation time when applying commutative functions (e.g. pearson's correlation).

Value

pairwise() returns a tibble with one column for each possible pairwise combination in .cols.

See Also

dplyr::across(), corrr::colpair_map(), {widyr}, recipes::step_interact(), recipes::step_ratio

Examples

library(dplyr)
library(pwiser)
library(palmerpenguins)

penguins <- na.omit(penguins)

# Grouped summary of correlations
penguins %>%
  group_by(species) %>%
  summarise(pairwise(contains("_mm"), ~stats::cor.test(.x, .y)$p.value, .is_commutative = TRUE),
            n = n())

# Building new columns
penguins %>%
  mutate(pairwise(contains("_mm"),
                  list(ratio = `/`, difference = `-`),
                  .names = "features_{.fn}_{.col_x}_{.col_y}"),
         n = n()) %>%
  glimpse()

brshallo/pwiser documentation built on July 7, 2022, 6:46 p.m.