courtiol · GitHub

Setting .keep = "used" in mutate() is the only settings for this argument that induces the drop of the grouping in both devel and CRAN versions.

library(dplyr, warn.conflicts = FALSE)
iris %>%
  group_by(Species) %>%
  mutate(meanPL = mean(Petal.Length), .keep = "none")
#> # A tibble: 150 x 2
#> # Groups:   Species [3]
#>    Species meanPL
#>    <fct>    <dbl>
#>  1 setosa    1.46
#>  2 setosa    1.46
#>  3 setosa    1.46
#>  4 setosa    1.46
#>  5 setosa    1.46
#>  6 setosa    1.46
#>  7 setosa    1.46
#>  8 setosa    1.46
#>  9 setosa    1.46
#> 10 setosa    1.46
#> # … with 140 more rows
iris %>%
  group_by(Species) %>%
  mutate(meanPL = mean(Petal.Length), .keep = "used")
#> # A tibble: 150 x 2
#>    Petal.Length meanPL
#>           <dbl>  <dbl>
#>  1          1.4   1.46
#>  2          1.4   1.46
#>  3          1.3   1.46
#>  4          1.5   1.46
#>  5          1.4   1.46
#>  6          1.7   1.46
#>  7          1.4   1.46
#>  8          1.5   1.46
#>  9          1.4   1.46
#> 10          1.5   1.46
#> # … with 140 more rows
packageVersion("dplyr")
#> [1] '1.0.2.9000'

Created on 2020-11-01 by the reprex package (v0.3.0)

Read the original on github.com ↗