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

Negated named variables used in across() can't be used with .by argument in mutate()/summarise() etc. #7007

Closed
heliconone opened this issue Mar 29, 2024 · 1 comment

Comments

@heliconone
Copy link

When using negated selections in across() (e.g. !a or !(a:b)) the negated variable(s) can't subsequently be used with the .by argument in dplyr's verbs.

library(dplyr, warn.conflicts = FALSE)

dat <- tibble(
  a = rep(c("a", "b"), 2),
  b = 1:4,
  c = 5:8
)

dat |>
  summarise(across(!a, mean), .by = a)
#> Error in `summarise()`:
#> ℹ In argument: `across(!a, mean)`.
#> Caused by error in `across()`:
#> ! Can't select columns that don't exist.
#> ✖ Column `a` doesn't exist.

Created on 2024-03-29 with reprex v2.1.0

There are many obvious workarounds but I would have imagined the above would work.

@DavisVaughan
Copy link
Member

The docs for across() mention:

You can't select grouping columns because they are already automatically handled by the verb

This includes doing negation. You should just assume that across() can't see a because its a grouping variable, and do everything() here instead

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

No branches or pull requests

2 participants