Skip to content

Commit

Permalink
Develop "quantile-unnest" example a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Apr 11, 2018
1 parent 45b31ee commit cce6e44
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ex07_group-by-summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ iris %>%
unnest() %>%
mutate(quantile = factor(quantile))

#' If something like this comes up a lot in an analysis, you could package the
#' key "moves" in a function, like so:
enquantile <- function(x, ...) {
qtile <- enframe(quantile(x, ...), name = "quantile")
qtile$quantile <- factor(qtile$quantile)
list(qtile)
}

#' This makes repeated downstream usage more concise.
iris %>%
group_by(Species) %>%
summarise(pl_qtile = enquantile(Petal.Length, c(0.25, 0.5, 0.75))) %>%
unnest()

32 changes: 32 additions & 0 deletions ex07_group-by-summarise.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,35 @@ iris %>%
#> 8 virginica 50% 5.55
#> 9 virginica 75% 5.88
```

If something like this comes up a lot in an analysis, you could package
the key “moves” in a function, like so:

``` r
enquantile <- function(x, ...) {
qtile <- enframe(quantile(x, ...), name = "quantile")
qtile$quantile <- factor(qtile$quantile)
list(qtile)
}
```

This makes repeated downstream usage more concise.

``` r
iris %>%
group_by(Species) %>%
summarise(pl_qtile = enquantile(Petal.Length, c(0.25, 0.5, 0.75))) %>%
unnest()
#> # A tibble: 9 x 3
#> Species quantile value
#> <fct> <fct> <dbl>
#> 1 setosa 25% 1.40
#> 2 setosa 50% 1.50
#> 3 setosa 75% 1.58
#> 4 versicolor 25% 4.00
#> 5 versicolor 50% 4.35
#> 6 versicolor 75% 4.60
#> 7 virginica 25% 5.10
#> 8 virginica 50% 5.55
#> 9 virginica 75% 5.88
```

0 comments on commit cce6e44

Please sign in to comment.