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

Cannot mutate grouping columns with across #5963

Closed
eutwt opened this issue Jul 30, 2021 · 1 comment
Closed

Cannot mutate grouping columns with across #5963

eutwt opened this issue Jul 30, 2021 · 1 comment

Comments

@eutwt
Copy link
Contributor

eutwt commented Jul 30, 2021

If a table is grouped by a column x, I can still alter the column with mutate using mutate(x = 2*x). However, if I try to do the same thing using across, I get an error saying that "Column x doesn't exist".

Is the fact that across cannot access grouping columns intentional?

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df <- tibble(x = rep(1:2, 2:3)) %>% group_by(x)

df %>%
  mutate(x = x*2)
#> # A tibble: 5 × 1
#> # Groups:   x [2]
#>       x
#>   <dbl>
#> 1     2
#> 2     2
#> 3     4
#> 4     4
#> 5     4

df %>%
  mutate(across(x, ~ . * 2))
#> Error: Problem with `mutate()` input `..1`.
#> ℹ `..1 = across(x, ~. * 2)`.
#> x Can't subset columns that don't exist.
#> x Column `x` doesn't exist.
#> ℹ The error occurred in group 1: x = 1.

Created on 2021-07-30 by the reprex package (v2.0.0)

@eutwt
Copy link
Contributor Author

eutwt commented Jul 30, 2021

Nevermind, should have checked the across documentation`:

"Because across() is used within functions like summarise() and mutate(), you can't select or compute upon grouping variables."

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

1 participant