Description Usage Arguments Value Examples
across2()
and across2x()
are variants of dplyr::across()
that iterate
over two columns simultaneously. across2()
loops each pair of columns in .xcols
and .ycols
over one or more functions, while across2x()
loops
every combination between columns in .xcols
and .ycols
over one or more functions.
1 2 3 4 5 6 7 8 9 10 11 |
.xcols, .ycols |
< |
.fns |
Functions to apply to each column in Possible values are:
Note that |
... |
Additional arguments for the function calls in |
.names |
A glue specification that describes how to name the output columns. This can use:
The default (
Alternatively to a glue specification, a character vector of length equal
to the number of columns to be created can be supplied to |
.names_fn |
Optionally, a function that is applied after the glue
specification in |
.comb |
In
|
across2()
returns a tibble with one column for each pair of elements in
.xcols
and .ycols
combined with each function in .fns
.
across2x()
returns a tibble with one column for each combination between
elements in .x
and.y
combined with each function in .fns
.
For the basic functionality of across()
please refer to the examples in
dplyr::across()
.
library(dplyr) # For better printing iris <- as_tibble(iris)
across2()
can be used to transfrom pairs of variables in one or more functions.
In the example below we want to calculate the product and the sum of all pairs of
'Length' and 'Width' variables. We can use {pre}
in the glue specification in
.names
to extract the common prefix of each pair of variables. We can further
transform the names, in the example setting them tolower
by specifying the
.names_fn
argument:
iris %>% transmute(across2(ends_with("Length"), ends_with("Width"), .fns = list(product = ~ .x * .y, sum = ~ .x + .y), .names = "{pre}_{fn}", .names_fn = tolower)) #> # A tibble: 150 x 4 #> sepal_product sepal_sum petal_product petal_sum #> <dbl> <dbl> <dbl> <dbl> #> 1 17.8 8.6 0.28 1.6 #> 2 14.7 7.9 0.28 1.6 #> 3 15.0 7.9 0.26 1.5 #> 4 14.3 7.7 0.3 1.7 #> # ... with 146 more rows
across2x()
can be used to perform calculations on each combination of variables.
In the example below we calculate the correlation between all variables in the
iris
data set for each group. To do this, we group_by
'Species' and specify
the tidyselect helper everything()
to .xcols
and .ycols
.
~ round(cor(.x, .y), 2)
gives us the correlation rounded to two digits for each
pair of variables. We trim the rahter long variables names by replacing "Sepal"
with "S", and "Petal" with "P" in the .names_fn
argument. Finally, we are not
interested in correlations of the same column and want to avoid excessive reults
by setting the .comb
argument to "minimal"
.
iris %>% group_by(Species) %>% summarise(across2x(everything(), everything(), ~ round(cor(.x, .y), 2), .names_fn = ~ gsub("Sepal", "S", .x) %>% gsub("Petal", "P", .), .comb = "minimal")) #> # A tibble: 3 x 7 #> Species S.Length_S.Width S.Length_P.Length S.Length_P.Width S.Width_P.Length #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 0.74 0.27 0.28 0.18 #> 2 versicolor 0.53 0.75 0.55 0.56 #> 3 virginica 0.46 0.86 0.28 0.4 #> # ... with 2 more variables: S.Width_P.Width <dbl>, P.Length_P.Width <dbl>
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.