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
- Host: GitHub
- URL: https://github.com/nacnudus/groupie
- Owner: nacnudus
- License: other
- Created: 2019-04-25T08:16:29.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-05-03T07:39:32.000Z (about 6 years ago)
- Last Synced: 2025-01-05T18:28:13.583Z (4 months ago)
- Language: R
- Homepage:
- Size: 19.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
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[](https://www.tidyverse.org/lifecycle/#retired)
[](https://cran.r-project.org/package=groupie)
[](https://travis-ci.org/nacnudus/groupie)
[](https://codecov.io/gh/nacnudus/groupie?branch=master)
[](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.