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

Ungroup after multi-level grouping to avoid warnings #891

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions episodes/12-dplyr.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,13 @@ lifeExp_bycountry %>%

:::::::::::::::::::::::::

The function `group_by()` allows us to group by multiple variables. Let's group by `year` and `continent`.
The function `group_by()` allows us to group by multiple variables. If you don't `ungroup()` after summarizing with multiple grouping variables, you may get a warning telling you the data are still partially grouped. Let's group by `year` and `continent`.

```{r}
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent, year) %>%
summarize(mean_gdpPercap = mean(gdpPercap))
summarize(mean_gdpPercap = mean(gdpPercap)) %>%
ungroup()
```

That is already quite powerful, but it gets even better! You're not limited to defining 1 new variable in `summarize()`.
Expand All @@ -297,7 +298,8 @@ gdp_pop_bycontinents_byyear <- gapminder %>%
summarize(mean_gdpPercap = mean(gdpPercap),
sd_gdpPercap = sd(gdpPercap),
mean_pop = mean(pop),
sd_pop = sd(pop))
sd_pop = sd(pop)) %>%
ungroup()
```

## count() and n()
Expand Down Expand Up @@ -350,7 +352,8 @@ gdp_pop_bycontinents_byyear <- gapminder %>%
mean_pop = mean(pop),
sd_pop = sd(pop),
mean_gdp_billion = mean(gdp_billion),
sd_gdp_billion = sd(gdp_billion))
sd_gdp_billion = sd(gdp_billion)) %>%
ungroup()
```

## Connect mutate with logical filtering: ifelse
Expand All @@ -371,15 +374,17 @@ gdp_pop_bycontinents_byyear_above25 <- gapminder %>%
mean_pop = mean(pop),
sd_pop = sd(pop),
mean_gdp_billion = mean(gdp_billion),
sd_gdp_billion = sd(gdp_billion))
sd_gdp_billion = sd(gdp_billion)) %>%
ungroup()

## updating only if certain condition is fullfilled
# for life expectations above 40 years, the gpd to be expected in the future is scaled
gdp_future_bycontinents_byyear_high_lifeExp <- gapminder %>%
mutate(gdp_futureExpectation = ifelse(lifeExp > 40, gdpPercap * 1.5, gdpPercap)) %>%
group_by(continent, year) %>%
summarize(mean_gdpPercap = mean(gdpPercap),
mean_gdpPercap_expected = mean(gdp_futureExpectation))
mean_gdpPercap_expected = mean(gdp_futureExpectation)) %>%
ungroup()
```

## Combining `dplyr` and `ggplot2`
Expand Down
Loading