tidyverse / dplyr

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

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

Closed sbanville-delfi closed 3 months ago

sbanville-delfi commented 3 months ago

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

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

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

@sbanville-delfi you might enjoy reading https://www.tidyverse.org/blog/2023/02/dplyr-1-1-0-per-operation-grouping/