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

Feature Request: add .groups = "drop" option to mutate #7066

Closed
sbanville-delfi opened this issue Aug 6, 2024 · 3 comments
Closed

Feature Request: add .groups = "drop" option to mutate #7066

sbanville-delfi opened this issue Aug 6, 2024 · 3 comments

Comments

@sbanville-delfi
Copy link

Currently summarise provides the .groups = "drop" parameter (along with other options), to eliminate the need to call ungroup. I think having this option available to mutate would also be helpful.

For example, it would be a useful shortcut to drop groups in mutate by doing the following:

library(ggplot2)
mpg |>
  dplyr::arrange(displ) |>
  dplyr::group_by(displ) |>
  dplyr::mutate(
    avg_hwy = mean(hwy),
    .groups = "drop"
  ) |>
  as.data.frame()

This isn't a high priority of course.

Thanks in advance.

@DavisVaughan
Copy link
Member

I think this is much nicer

library(ggplot2)
mpg |>
  dplyr::arrange(displ) |>
  dplyr::mutate(
    avg_hwy = mean(hwy),
    .by = displ
  ) |>
  as.data.frame()

Same for summarise(), really, instead of having to specify .groups.

For summarise(), we only introduced .groups because we were already doing some "special things" with the groups before we returned them, and this gave the user a way to control that. For mutate(), if you pass in a data frame grouped by group_by() then there is only ever 1 behavior - it retains those groups. So I don't think there is any need to add the same kind of customization argument here. But thanks!

@sbanville-delfi
Copy link
Author

I think this is much nicer

library(ggplot2)
mpg |>
  dplyr::arrange(displ) |>
  dplyr::mutate(
    avg_hwy = mean(hwy),
    .by = displ
  ) |>
  as.data.frame()

Same for summarise(), really, instead of having to specify .groups.

For summarise(), we only introduced .groups because we were already doing some "special things" with the groups before we returned them, and this gave the user a way to control that. For mutate(), if you pass in a data frame grouped by group_by() then there is only ever 1 behavior - it retains those groups. So I don't think there is any need to add the same kind of customization argument here. But thanks!

Thanks @DavisVaughan for the quick response!
Yes, agreed. I didn't realize this was doable already in mutate; now I know.

@DavisVaughan
Copy link
Member

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