These functions show the prefixes or suffixes for each pair of variables of two sets of columns. They are intended to be used either (1) in case across2 throws an error when {pre} or {suf} are specified in across2's .names argument or (2) before using {pre} or {suf} in across2 to understand how the pre- or suffixes will look like.

  • show_prefix() lists each variable pair and the corresponding alphanumeric prefix

  • show_suffix() lists each variable pair and the corresponding alphanumeric suffix

show_prefix(.data = NULL, .xcols = NULL, .ycols = NULL)

show_suffix(.data = NULL, .xcols = NULL, .ycols = NULL)

Arguments

.data

A data frame.

.xcols, .ycols

<tidy-select> Sets of columns for which the common pre- or suffix will be shown for each pair. Note that you can not select.

Value

A tibble with three columns: .xcols, .ycols and prefix or suffix.

Examples

Below two use cases of show_prefix/suffix are briefly explained. Let's first attach dplyr and get ready:

library(dplyr)

# For better printing
iris <- as_tibble(iris)

(1) When called after an error is thrown by across2()

Let's assume we use across2 with the {pre} glue specification on some data where not all variable pairs share a common prefix. In the example below we use dplyr::rename to create such a case. Then across2 will throw an error. The error message already suggests that we can run show_prefix() to see what went wrong. In this case we can call show_prefix() without any arguments:

 iris %>%
   as_tibble %>%
   rename("Pesal.Length" = Sepal.Length) %>%
   mutate(across2(ends_with("Length"),
                  ends_with("Width"),
                  .fns = list(product = ~ .x * .y,
                              sum = ~ .x + .y),
                  .names = "{pre}_{fn}"))
#> Error: Problem with `mutate()` input `..1`.
#> i `..1 = across2(...)`.
#> x Problem with `across2()` input `.names`.
#> i When `{pre}` is used inside `.names` each pair of input variables in `.xcols` and `.ycols` must share a common prefix of length > 0.
#> x For at least one pair of variables a shared prefix could not be extracted.
#> i Run `show_prefix()` to see the prefixes for each variable pair.
show_prefix()
#> # A tibble: 2 x 3
#>   .xcols       .ycols      prefix
#>   <chr>        <chr>       <chr> 
#> 1 Pesal.Length Sepal.Width <NA>  
#> 2 Petal.Length Petal.Width Petal

(2) When called on a data.frame

When called on a data.frame we just need to specify two sets of columns: .xcols and .ycols (just like in across2).

 iris %>%
   show_suffix(starts_with("Sepal"),
               starts_with("Petal"))
#> # A tibble: 2 x 3
#>   .xcols       .ycols       suffix
#>   <chr>        <chr>        <chr> 
#> 1 Sepal.Length Petal.Length Length
#> 2 Sepal.Width  Petal.Width  Width