@group_by
Grouping and ungrouping behavior is one of the nicest parts of using R's tidyverse. Once a data frame is grouped, all verbs applied to that data frame respect the grouping, including but not limited to @mutate()
, @summarize()
, @slice()
and @filter
, which allows for really powerful abstractions. For example, with @group_by()
followed by @filter()
, you can limit the rows of a dataset to the maximum or minimum values for each group.
Exactly as in R's tidyverse
, once a data frame is grouped, it remains grouped until either @summarize()
is called (which "peels off" one layer of grouping) or @ungroup()
is called, which removes all layers of grouping. Also as in R's tidyverse
, @group_by()
sorts the groups in ascending order. Unlike in R, there is never any question about whether a data frame is currently grouped because GroupedDataFrames print out in a very different form than DataFrames, making them easy to tell apart.
When using @chain
, note that you can write either @ungroup
or @ungroup()
. Both are considered valid.
using Tidier
using RDatasets
movies = dataset("ggplot2", "movies");
Combining @group_by()
with @mutate()
¤
@chain movies begin
@group_by(Year)
@mutate(Mean_Yearly_Rating = mean(skipmissing(Rating)))
@select(Year, Rating, Mean_Yearly_Rating)
@ungroup
@slice(1:5)
end
Row | Year | Rating | Mean_Yearly_Rating |
---|---|---|---|
Int32 | Float64 | Float64 | |
1 | 1893 | 7.0 | 7.0 |
2 | 1894 | 4.6 | 4.88889 |
3 | 1894 | 5.0 | 4.88889 |
4 | 1894 | 5.8 | 4.88889 |
5 | 1894 | 4.2 | 4.88889 |
Combining @group_by() with @summarize()¤
@chain movies begin
@group_by(Year)
@summarize(Mean_Yearly_Rating = mean(skipmissing(Rating)),
Median_Yearly_Rating = median(skipmissing(Rating)))
@slice(1:5)
end
Row | Year | Mean_Yearly_Rating | Median_Yearly_Rating |
---|---|---|---|
Int32 | Float64 | Float64 | |
1 | 1893 | 7.0 | 7.0 |
2 | 1894 | 4.88889 | 4.6 |
3 | 1895 | 5.5 | 5.7 |
4 | 1896 | 5.26923 | 5.3 |
5 | 1897 | 4.67778 | 4.6 |
Grouping by multiple columns¤
@chain movies begin
@group_by(Year, Comedy)
@summarize(Mean_Yearly_Rating = mean(skipmissing(Rating)),
Median_Yearly_Rating = median(skipmissing(Rating)))
@ungroup # Need to ungroup to peel off grouping by Year
@arrange(desc(Year), Comedy)
@slice(1:5)
end
Row | Year | Comedy | Mean_Yearly_Rating | Median_Yearly_Rating |
---|---|---|---|---|
Int32 | Int32 | Float64 | Float64 | |
1 | 2005 | 0 | 6.62788 | 6.75 |
2 | 2005 | 1 | 6.30081 | 6.1 |
3 | 2004 | 0 | 6.76521 | 6.9 |
4 | 2004 | 1 | 6.42898 | 6.6 |
5 | 2003 | 0 | 6.40409 | 6.6 |
Combining @group_by() with @filter()¤
@chain movies begin
@group_by(Year)
@filter(Rating == minimum(Rating))
@ungroup
@select(Year, Rating)
@arrange(desc(Year))
@slice(1:10)
end
Row | Year | Rating |
---|---|---|
Int32 | Float64 | |
1 | 2005 | 1.8 |
2 | 2004 | 1.0 |
3 | 2004 | 1.0 |
4 | 2004 | 1.0 |
5 | 2004 | 1.0 |
6 | 2004 | 1.0 |
7 | 2004 | 1.0 |
8 | 2004 | 1.0 |
9 | 2003 | 1.0 |
10 | 2003 | 1.0 |
This page was generated using Literate.jl.