Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirm if dplyr::across can pass same length of vector to .names #6895

Closed
ynsec37 opened this issue Jul 25, 2023 · 1 comment · Fixed by #6953
Closed

Confirm if dplyr::across can pass same length of vector to .names #6895

ynsec37 opened this issue Jul 25, 2023 · 1 comment · Fixed by #6953

Comments

@ynsec37
Copy link

ynsec37 commented Jul 25, 2023

Dear developer,

I found the below example work well but no related description in current official document.

.names A glue specification that describes how to name the output columns. This can use {.col} to stand for the selected column name, and {.fn} to stand for the name of the function being applied. The default (NULL) is equivalent to "{.col}" for the single function case and "{.col}_{.fn}" for the case where a list is used for .fns.

I just want to confirm whether this way is right or accepted, especilly when the new variable names can not simply be computed by {.col} or {.fn}

library(dplyr, warn.conflicts = FALSE)

df <- tibble::tibble(a = 1, b = 2, c = 3)

cols <- c("a", "b", "c")
new_cols <- c("x", "y", "z")

df %>%
  mutate(across(
    .cols = all_of(cols),
    .fns = ~ .x + 10,
    .names = "{new_cols}"
  ))
#> # A tibble: 1 × 6
#>       a     b     c     x     y     z
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1     1     2     3    11    12    13

Thank very much for your reply.

@moodymudskipper
Copy link

Since the doc says it's a glue specification I believe the behavior is accepted (though happy to see it confirmed), but maybe the intent might be made clearer by either of those 2, though these might also slightly bite into the undocumented territory.

cols <- c("a", "b", "c")
new_cols <- c("x", "y", "z")

df %>%
  mutate(
    across(
    .cols = all_of(cols),
    .fns = ~ .x + 10
  ) %>% set_names(new_cols)
)

df %>%
  mutate(
    across(
      .cols = all_of(set_names(cols, new_cols)),
      .fns = ~ .x + 10
    )
  )

The former uses the fact that across returns a data frame, so should be stable, unless across() is changed to only be recognised at the top level in dplyr verbs (it's not very clear from the doc if only top level is expected).

The latter is not 100% documented either but pretty close through #6655

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants