-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of group_by + summarise and the list() trick
thx @hadley
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#' --- | ||
#' title: "Work on groups of rows via dplyr::group_by() + summarise()" | ||
#' author: "Jenny Bryan" | ||
#' date: "`r format(Sys.Date())`" | ||
#' output: github_document | ||
#' --- | ||
|
||
#+ setup, include = FALSE, cache = FALSE | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>", | ||
error = TRUE | ||
) | ||
options(tidyverse.quiet = TRUE) | ||
|
||
#+ body | ||
# ---- | ||
|
||
#' What if you need to work on groups of rows? Such as the groups induced by | ||
#' the levels of a factor. | ||
#' | ||
#' You do not need to ... split the data frame into mini-data-frames, loop over | ||
#' them, and glue it all back together. | ||
#' | ||
#' Instead, use `dplyr::group_by()`, followed by `dplyr::summarize()`, to | ||
#' compute group-wise summaries. | ||
|
||
library(dplyr) | ||
|
||
iris %>% | ||
group_by(Species) %>% | ||
summarise(pl_avg = mean(Petal.Length), pw = mean(Petal.Width)) | ||
|
||
#' What if you want to return summaries that are not just a single number? | ||
#' | ||
#' This does not "just work". | ||
iris %>% | ||
group_by(Species) %>% | ||
summarise(pl_qtile = quantile(Petal.Length, c(0.25, 0.5, 0.75))) | ||
|
||
#' Solution: package as a length-1 list that contains 3 values, creating a | ||
#' list-column. | ||
iris %>% | ||
group_by(Species) %>% | ||
summarise(pl_qtile = list(quantile(Petal.Length, c(0.25, 0.5, 0.75)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
Work on groups of rows via dplyr::group\_by() + summarise() | ||
================ | ||
Jenny Bryan | ||
2018-04-10 | ||
|
||
What if you need to work on groups of rows? Such as the groups induced | ||
by the levels of a factor. | ||
|
||
You do not need to … split the data frame into mini-data-frames, loop | ||
over them, and glue it all back together. | ||
|
||
Instead, use `dplyr::group_by()`, followed by `dplyr::summarize()`, to | ||
compute group-wise summaries. | ||
|
||
``` r | ||
library(dplyr) | ||
#> | ||
#> Attaching package: 'dplyr' | ||
#> The following objects are masked from 'package:stats': | ||
#> | ||
#> filter, lag | ||
#> The following objects are masked from 'package:base': | ||
#> | ||
#> intersect, setdiff, setequal, union | ||
|
||
iris %>% | ||
group_by(Species) %>% | ||
summarise(pl_avg = mean(Petal.Length), pw = mean(Petal.Width)) | ||
#> # A tibble: 3 x 3 | ||
#> Species pl_avg pw | ||
#> <fct> <dbl> <dbl> | ||
#> 1 setosa 1.46 0.246 | ||
#> 2 versicolor 4.26 1.33 | ||
#> 3 virginica 5.55 2.03 | ||
``` | ||
|
||
What if you want to return summaries that are not just a single number? | ||
|
||
This does not “just work”. | ||
|
||
``` r | ||
iris %>% | ||
group_by(Species) %>% | ||
summarise(pl_qtile = quantile(Petal.Length, c(0.25, 0.5, 0.75))) | ||
#> Error in summarise_impl(.data, dots): Column `pl_qtile` must be length 1 (a summary value), not 3 | ||
``` | ||
|
||
Solution: package as a length-1 list that contains 3 values, creating a | ||
list-column. | ||
|
||
``` r | ||
iris %>% | ||
group_by(Species) %>% | ||
summarise(pl_qtile = list(quantile(Petal.Length, c(0.25, 0.5, 0.75)))) | ||
#> # A tibble: 3 x 2 | ||
#> Species pl_qtile | ||
#> <fct> <list> | ||
#> 1 setosa <dbl [3]> | ||
#> 2 versicolor <dbl [3]> | ||
#> 3 virginica <dbl [3]> | ||
``` |