-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
122 lines (94 loc) · 3.13 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(dplyr)
library(purrr)
library(groupie)
```
# groupie
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#retired)
[![CRAN status](https://www.r-pkg.org/badges/version/groupie)](https://cran.r-project.org/package=groupie)
[![Travis build status](https://travis-ci.org/nacnudus/groupie.svg?branch=master)](https://travis-ci.org/nacnudus/groupie)
[![Codecov test coverage](https://codecov.io/gh/nacnudus/groupie/branch/master/graph/badge.svg)](https://codecov.io/gh/nacnudus/groupie?branch=master)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/nacnudus/groupie?branch=master&svg=true)](https://ci.appveyor.com/project/nacnudus/groupie)
<!-- badges: end -->
The groupie package provides `groups_split()` to create copies of a data frame
with different combinations of grouping variables. This is useful for
summarising data several ways in one go.
## Caveat
This package is almost certainly overkill. You can easily implement the
functions yourself.
The trick is to treat sets of grouping variables as 'data', map over them to
create copies of your actual data grouped by each set of variables, and then map
again with a `summarise()` function or whatever.
```{r}
library(dplyr)
library(purrr)
grouping_vars <- c("vs", "am", "carb")
map(grouping_vars, group_by_at, .tbl = mtcars) %>%
map(summarise,
'6 cylinder' = sum(cyl == 6),
'Large disp' = sum(disp >= 100),
'low gears' = sum(gear <= 4))
```
This package also wraps the
[arrangements](https://cran.r-project.org/package=arrangements) package to
create sets of grouping variables. This again is very simple to do yourself.
## Installation
You can install the development version of groupie from github with
``` r
# install.packages("remotes") # if not already installed
remotes::install_github("nacnudus/groupie")
```
## Example
```{r library}
library(groupie)
library(dplyr)
library(purrr)
```
Summarise by
* `cyl`
* `am`
* `mpg`
```{r individuals}
mtcars %>%
group_by(cyl, am, gear) %>%
groups_split(individuals) %>%
map(summarise, mpg = mean(mpg))
```
* `cyl`
* `cyl` and `am`
* `cyl`, `am` and `mpg`
```{r hierarchy}
mtcars %>%
group_by(cyl, am, gear) %>%
groups_split(hierarchy) %>%
map(summarise, mpg = mean(mpg))
```
Summarise by each one of `cyl`, `am` and `mpg`.
```{r k_combinations_1}
mtcars %>%
group_by(cyl, am, gear) %>%
groups_split(k_combinations, 1L) %>%
map(summarise, mpg = mean(mpg))
```
Summarise by all pairs of `cyl`, `am` and `mpg`.
```{r k_combinations_2}
mtcars %>%
group_by(cyl, am, gear) %>%
groups_split(k_combinations, 2L) %>%
map(summarise, mpg = mean(mpg))
```
## Contribute
Please note that the 'groupie' project is released with a
[Contributor Code of Conduct](.github/CODE_OF_CONDUCT.md).
By contributing to this project, you agree to abide by its terms.