-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Allow ungroup to specify removal of grouping variable #3760
Comments
🤔 > dplyr:::ungroup.grouped_df
function(x, ...) {
ungroup_grouped_df(x)
}
<bytecode: 0x1026547e8>
<environment: namespace:dplyr> but I'm not sure about having |
Seems to me that incorporating this kind of logic into #3721 would be the better solution for this use case. I do think it would be neat if
would be equivalent to
which is how I first interpreted the title of this issue. |
Here is another example taken from https://stackoverflow.com/questions/52906985/merging-of-duplicate-rows-that-have-misspelled-variables/52907932#52907932
With the feature under discussion this would simplify to the shorter and more symmetric:
|
@mkoohafkan, The way I suppose there is the question of whether Another possibility is to use One other point is that I don't think incrementally adding and removing parts of a |
@ggrothendieck thought about this more and I agree with your statements that
|
dplyr::group_by(mtcars, -cyl)
#> # A tibble: 32 x 12
#> # Groups: -cyl [3]
#> mpg cyl disp hp drat wt qsec vs am gear carb `-cyl`
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 -6
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 -6
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 -4
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 -6
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 -8
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 -6
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 -8
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 -4
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 -4
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 -6
#> # ... with 22 more rows Created on 2018-10-31 by the reprex package (v0.2.1) So, to me, |
Here is another case where this feature could be used taken from https://stackoverflow.com/questions/53240324/dplyr-collapse-tail-rows-into-larger-groups/53240699#53240699 Note how this keeps coming up again and again.
|
Having a selective ungroup is also very import when calculating percentages of subgroups.
|
I think it would be fine for |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
A common case is that one constructs a grouping variable in
group_by
but only needs it for the duration of thegroup_by
so afterwards one must useselect
to get rid of it as in the example below. It would be pleasingly symmetric ifungroup
could remove the added column just asgroup_by
adds it sowould be the same as
Thus in this example taken from https://stackoverflow.com/questions/51939874/referencing-previous-column-value-as-column-is-created/51940343#51940343
we could write using one fewer statement, i.e. the last two lines of code above are combined into the last line below.
Note the reduced line count and improved symmetry.
The text was updated successfully, but these errors were encountered: