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

select() silently refuses to drop grouping variable #1511

Closed
jennybc opened this issue Nov 5, 2015 · 7 comments
Closed

select() silently refuses to drop grouping variable #1511

jennybc opened this issue Nov 5, 2015 · 7 comments
Labels
Milestone

Comments

@jennybc
Copy link
Member

@jennybc jennybc commented Nov 5, 2015

Maybe this is just documentation, but a warning would be nice. I know I can ungroup() and I shall.

x <- data.frame(y = 1:3, z = LETTERS[3:1])
x %>%
  group_by(z) %>% 
  select(-z)
#> Source: local data frame [3 x 2]
#> Groups: z [3]
#> 
#>        z     y
#>   (fctr) (int)
#> 1      C     1
#> 2      B     2
#> 3      A     3

Same issue on stack overflow here and here.

@hadley
Copy link
Member

@hadley hadley commented Nov 5, 2015

What do you expect to happen?

a) fail with a warning
b) remove z and hence remove the grouping
c) remove z and somehow keep the grouping

Loading

@jennybc
Copy link
Member Author

@jennybc jennybc commented Nov 5, 2015

I think a) or b) is good. For me, the biggest thing is the wake up call that a warning provides.

Loading

@hadley
Copy link
Member

@hadley hadley commented Mar 7, 2016

I'm leaning towards making this warning. Something along the lines of "Automatically adding z to variable list to preserve grouping".

Would you expect the same warning if you did:

x %>%
  group_by(z) %>% 
  select(x)

Loading

@jennybc
Copy link
Member Author

@jennybc jennybc commented Mar 7, 2016

Yes I would expect and appreciate that warning.

In real usage, the group_by() can be much farther away and I often forget that I've grouped, once the groups have served their purpose. Maybe I should always ungroup(), if no immediate reason to keep grouping.

Loading

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Mar 7, 2016

Going a bit further: What are the use cases for more than one verb between group_by() and ungroup()? Perhaps summarize() and do() should always ungroup?

Loading

@hadley
Copy link
Member

@hadley hadley commented Mar 7, 2016

@krlmlr I take advantage of it quite a bit, plus I think it's too late to change now.

Loading

@hadley hadley closed this in 44a22c6 Mar 7, 2016
@DieselAnalytics
Copy link

@DieselAnalytics DieselAnalytics commented Sep 17, 2016

@jennybc thanks for posting this issue. I thought this was an bug but now I know to use "ungroup". @hadley thanks for developing the package!

Loading

@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants