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)
.data | A data frame. |
---|---|
.xcols, .ycols | < |
A tibble with three columns: .xcols, .ycols and prefix or suffix.
Below two use cases of show_prefix/suffix
are briefly explained.
Let's first attach dplyr and get ready:
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
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