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

Use of count() in example in Factors chapter now broken due to change in grouping #610

Closed
tmelconian opened this Issue Sep 20, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@tmelconian
Copy link

tmelconian commented Sep 20, 2017

Referring to the following example in the factors chapter:

by_age <- gss_cat %>%
  filter(!is.na(age)) %>%
  group_by(age, marital) %>%
  count() %>%
  mutate(prop = n / sum(n))

To work correctly, this example requires that count() will remove one group_by level, like summarise does. This was true when the book was written, and true as of dplyr 0.5, but is no longer true today, apparently due to tidyverse/dplyr@439e54d and/or related changes.

Could you please update the book and explicitly note that this behavior has changed since earlier versions of dplyr? It confuses students to have an example which doesn't work, and they already find the implicit removal of group_by levels confusing to start with.

The following variant works for me on both current and 0.50 dplyr:

gss_cat %>%
    filter(!is.na(age)) %>%
    group_by(age) %>%
    count(marital) %>%
    mutate(prop=n/sum(n))
@hadley

This comment has been minimized.

Copy link
Owner

hadley commented Jun 20, 2018

Now fixed, thanks.

@hadley hadley closed this Jun 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment