Closed sbanville-delfi closed 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!
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. Formutate()
, if you pass in a data frame grouped bygroup_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.
@sbanville-delfi you might enjoy reading https://www.tidyverse.org/blog/2023/02/dplyr-1-1-0-per-operation-grouping/
Currently
summarise
provides the.groups = "drop"
parameter (along with other options), to eliminate the need to callungroup
. I think having this option available tomutate
would also be helpful.For example, it would be a useful shortcut to drop groups in mutate by doing the following:
This isn't a high priority of course.
Thanks in advance.