pairwise | R Documentation |
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()
).
pairwise( .cols = everything(), .fns = NULL, ..., .names = NULL, .is_commutative = FALSE )
.cols |
< |
.fns |
Functions to apply to each pair of the selected columns. Possible values are:
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 |
... |
Additional arguments for the function calls in |
.names |
A glue specification that describes how to name the outputted
columns. Use Default format when When |
.is_commutative |
If |
pairwise()
returns a tibble with one column for each possible pairwise combination in .cols
.
dplyr::across()
, corrr::colpair_map()
, {widyr}
, recipes::step_interact()
, recipes::step_ratio
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.