tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.79k stars 2.12k forks source link

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

Closed heliconone closed 8 months ago

heliconone commented 8 months ago

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 commented 8 months ago

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