An open API service indexing awesome lists of open source software.

https://github.com/nacnudus/groupie

Summarise data frames by different combinations of grouping variables
https://github.com/nacnudus/groupie

Last synced: 2 months ago
JSON representation

Summarise data frames by different combinations of grouping variables

Awesome Lists containing this project

README

        

---
output: github_document
---

```{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

[![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)

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.