Skip to contents

Create a new data frame including duplicate rows where the rows fulfil a potentially overlapping set of conditions specified as named predicates (as formulae)

Usage

intersecting_group_by(.data, ..., .colname)

Arguments

.data

a data frame

...

a set of predicates specified like case_whens syntax, such as mpg < 5 ~ "gas guzzlers"

.colname

the name of the new group

Value

a new dataframe containing the overlapping groups which may create duplicates of individual rows.

Examples

library(tidyverse)
iris %>% dplyr::group_by(Species) %>% intersecting_group_by(
  Sepal.Length > mean(Sepal.Length) ~ "Long",
  Sepal.Width > mean(Sepal.Width) ~ "Wide"
)
#> # A tibble: 148 × 6
#> # Groups:   Species, group [6]
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species group
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>   <chr>
#>  1          5.1         3.5          1.4         0.2 setosa  Long 
#>  2          5.4         3.9          1.7         0.4 setosa  Long 
#>  3          5.4         3.7          1.5         0.2 setosa  Long 
#>  4          5.8         4            1.2         0.2 setosa  Long 
#>  5          5.7         4.4          1.5         0.4 setosa  Long 
#>  6          5.4         3.9          1.3         0.4 setosa  Long 
#>  7          5.1         3.5          1.4         0.3 setosa  Long 
#>  8          5.7         3.8          1.7         0.3 setosa  Long 
#>  9          5.1         3.8          1.5         0.3 setosa  Long 
#> 10          5.4         3.4          1.7         0.2 setosa  Long 
#> # ℹ 138 more rows