Separate a data.frame into a list of any depth by one or more stratification columns whose levels become the names.

divide(
    data,
    ...,
    depth = Inf,
    remove = TRUE,
    drop = TRUE,
    sep = "|"
)

Arguments

data

Any data.frame.

...

Selection of columns to split by. See dplyr::select for details.

depth

Depth to split to. Defaults to Inf. See details for more information.

remove

Should the stratfication columns be removed? Defaults to TRUE.

drop

Should unused combinations of stratification variables be dropped? Defaults to TRUE.

sep

String to separate values of each stratification variable by. Defaults to "|". Only used when the number of stratification columns exceeds the desired depth.

Details

For the depth, use positive integers to move from the root and negative integers to move from the leaves. The maximum (minimum) depth will be used for integers larger (smaller) than such.

Value

A list

Author

Alex Zajichek

Examples

#Unquoted selection
heart_disease %>%
    divide(
        Sex
    )
#> $Female
#> # A tibble: 97 × 8
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR Exerc…¹ Heart…²
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>   <fct>  
#>  1    41 Atypical angina    130         204 FALSE            172 No      No     
#>  2    62 Asymptomatic       140         268 FALSE            160 No      Yes    
#>  3    57 Asymptomatic       120         354 FALSE            163 Yes     No     
#>  4    56 Atypical angina    140         294 FALSE            153 No      No     
#>  5    48 Non-anginal pain   130         275 FALSE            139 No      No     
#>  6    58 Typical angina     150         283 TRUE             162 No      No     
#>  7    50 Non-anginal pain   120         219 FALSE            158 No      No     
#>  8    58 Non-anginal pain   120         340 FALSE            172 No      No     
#>  9    66 Typical angina     150         226 FALSE            114 No      No     
#> 10    69 Typical angina     140         239 FALSE            151 No      No     
#> # … with 87 more rows, and abbreviated variable names ¹​ExerciseInducedAngina,
#> #   ²​HeartDisease
#> 
#> $Male
#> # A tibble: 206 × 8
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR Exerc…¹ Heart…²
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>   <fct>  
#>  1    63 Typical angina     145         233 TRUE             150 No      No     
#>  2    67 Asymptomatic       160         286 FALSE            108 Yes     Yes    
#>  3    67 Asymptomatic       120         229 FALSE            129 Yes     Yes    
#>  4    37 Non-anginal pain   130         250 FALSE            187 No      No     
#>  5    56 Atypical angina    120         236 FALSE            178 No      No     
#>  6    63 Asymptomatic       130         254 FALSE            147 No      Yes    
#>  7    53 Asymptomatic       140         203 TRUE             155 Yes     Yes    
#>  8    57 Asymptomatic       140         192 FALSE            148 No      No     
#>  9    56 Non-anginal pain   130         256 TRUE             142 Yes     Yes    
#> 10    44 Atypical angina    120         263 FALSE            173 No      No     
#> # … with 196 more rows, and abbreviated variable names ¹​ExerciseInducedAngina,
#> #   ²​HeartDisease
#> 

#Using select helpers
heart_disease %>%
    divide(
        matches("^S")
    )
#> $Female
#> # A tibble: 97 × 8
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR Exerc…¹ Heart…²
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>   <fct>  
#>  1    41 Atypical angina    130         204 FALSE            172 No      No     
#>  2    62 Asymptomatic       140         268 FALSE            160 No      Yes    
#>  3    57 Asymptomatic       120         354 FALSE            163 Yes     No     
#>  4    56 Atypical angina    140         294 FALSE            153 No      No     
#>  5    48 Non-anginal pain   130         275 FALSE            139 No      No     
#>  6    58 Typical angina     150         283 TRUE             162 No      No     
#>  7    50 Non-anginal pain   120         219 FALSE            158 No      No     
#>  8    58 Non-anginal pain   120         340 FALSE            172 No      No     
#>  9    66 Typical angina     150         226 FALSE            114 No      No     
#> 10    69 Typical angina     140         239 FALSE            151 No      No     
#> # … with 87 more rows, and abbreviated variable names ¹​ExerciseInducedAngina,
#> #   ²​HeartDisease
#> 
#> $Male
#> # A tibble: 206 × 8
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR Exerc…¹ Heart…²
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>   <fct>  
#>  1    63 Typical angina     145         233 TRUE             150 No      No     
#>  2    67 Asymptomatic       160         286 FALSE            108 Yes     Yes    
#>  3    67 Asymptomatic       120         229 FALSE            129 Yes     Yes    
#>  4    37 Non-anginal pain   130         250 FALSE            187 No      No     
#>  5    56 Atypical angina    120         236 FALSE            178 No      No     
#>  6    63 Asymptomatic       130         254 FALSE            147 No      Yes    
#>  7    53 Asymptomatic       140         203 TRUE             155 Yes     Yes    
#>  8    57 Asymptomatic       140         192 FALSE            148 No      No     
#>  9    56 Non-anginal pain   130         256 TRUE             142 Yes     Yes    
#> 10    44 Atypical angina    120         263 FALSE            173 No      No     
#> # … with 196 more rows, and abbreviated variable names ¹​ExerciseInducedAngina,
#> #   ²​HeartDisease
#> 

#Reduced depth
heart_disease %>%
    divide(
        Sex,
        HeartDisease,
        depth = 1
    )
#> $`Female|No`
#> # A tibble: 72 × 7
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR ExerciseInduc…¹
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>          
#>  1    41 Atypical angina    130         204 FALSE            172 No             
#>  2    57 Asymptomatic       120         354 FALSE            163 Yes            
#>  3    56 Atypical angina    140         294 FALSE            153 No             
#>  4    48 Non-anginal pain   130         275 FALSE            139 No             
#>  5    58 Typical angina     150         283 TRUE             162 No             
#>  6    50 Non-anginal pain   120         219 FALSE            158 No             
#>  7    58 Non-anginal pain   120         340 FALSE            172 No             
#>  8    66 Typical angina     150         226 FALSE            114 No             
#>  9    69 Typical angina     140         239 FALSE            151 No             
#> 10    71 Atypical angina    160         302 FALSE            162 No             
#> # … with 62 more rows, and abbreviated variable name ¹​ExerciseInducedAngina
#> 
#> $`Male|No`
#> # A tibble: 92 × 7
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR ExerciseInduc…¹
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>          
#>  1    63 Typical angina     145         233 TRUE             150 No             
#>  2    37 Non-anginal pain   130         250 FALSE            187 No             
#>  3    56 Atypical angina    120         236 FALSE            178 No             
#>  4    57 Asymptomatic       140         192 FALSE            148 No             
#>  5    44 Atypical angina    120         263 FALSE            173 No             
#>  6    52 Non-anginal pain   172         199 TRUE             162 No             
#>  7    57 Non-anginal pain   150         168 FALSE            174 No             
#>  8    54 Asymptomatic       140         239 FALSE            160 No             
#>  9    49 Atypical angina    130         266 FALSE            171 No             
#> 10    64 Typical angina     110         211 FALSE            144 Yes            
#> # … with 82 more rows, and abbreviated variable name ¹​ExerciseInducedAngina
#> 
#> $`Female|Yes`
#> # A tibble: 25 × 7
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR ExerciseInduc…¹
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>          
#>  1    62 Asymptomatic       140         268 FALSE            160 No             
#>  2    65 Asymptomatic       150         225 FALSE            114 No             
#>  3    61 Asymptomatic       130         330 FALSE            169 No             
#>  4    51 Asymptomatic       130         305 FALSE            142 Yes            
#>  5    62 Asymptomatic       160         164 FALSE            145 No             
#>  6    60 Asymptomatic       150         258 FALSE            157 No             
#>  7    61 Asymptomatic       145         307 FALSE            146 Yes            
#>  8    43 Asymptomatic       132         341 TRUE             136 Yes            
#>  9    62 Non-anginal pain   130         263 FALSE             97 No             
#> 10    63 Asymptomatic       150         407 FALSE            154 No             
#> # … with 15 more rows, and abbreviated variable name ¹​ExerciseInducedAngina
#> 
#> $`Male|Yes`
#> # A tibble: 114 × 7
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR ExerciseInduc…¹
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>          
#>  1    67 Asymptomatic       160         286 FALSE            108 Yes            
#>  2    67 Asymptomatic       120         229 FALSE            129 Yes            
#>  3    63 Asymptomatic       130         254 FALSE            147 No             
#>  4    53 Asymptomatic       140         203 TRUE             155 Yes            
#>  5    56 Non-anginal pain   130         256 TRUE             142 Yes            
#>  6    48 Atypical angina    110         229 FALSE            168 No             
#>  7    58 Atypical angina    120         284 FALSE            160 No             
#>  8    58 Non-anginal pain   132         224 FALSE            173 No             
#>  9    60 Asymptomatic       130         206 FALSE            132 Yes            
#> 10    40 Asymptomatic       110         167 FALSE            114 Yes            
#> # … with 104 more rows, and abbreviated variable name ¹​ExerciseInducedAngina
#> 
    
#Keep columns in result; change delimiter in names
heart_disease %>%
    divide(
        Sex,
        HeartDisease,
        depth = 1,
        remove = FALSE,
        sep = ","
    )
#> $`Female,No`
#> # A tibble: 72 × 9
#>      Age Sex    ChestPain           BP Cholest…¹ Blood…² Maxim…³ Exerc…⁴ Heart…⁵
#>    <dbl> <fct>  <fct>            <dbl>     <dbl> <lgl>     <dbl> <fct>   <fct>  
#>  1    41 Female Atypical angina    130       204 FALSE       172 No      No     
#>  2    57 Female Asymptomatic       120       354 FALSE       163 Yes     No     
#>  3    56 Female Atypical angina    140       294 FALSE       153 No      No     
#>  4    48 Female Non-anginal pain   130       275 FALSE       139 No      No     
#>  5    58 Female Typical angina     150       283 TRUE        162 No      No     
#>  6    50 Female Non-anginal pain   120       219 FALSE       158 No      No     
#>  7    58 Female Non-anginal pain   120       340 FALSE       172 No      No     
#>  8    66 Female Typical angina     150       226 FALSE       114 No      No     
#>  9    69 Female Typical angina     140       239 FALSE       151 No      No     
#> 10    71 Female Atypical angina    160       302 FALSE       162 No      No     
#> # … with 62 more rows, and abbreviated variable names ¹​Cholesterol,
#> #   ²​BloodSugar, ³​MaximumHR, ⁴​ExerciseInducedAngina, ⁵​HeartDisease
#> 
#> $`Male,No`
#> # A tibble: 92 × 9
#>      Age Sex   ChestPain           BP Choleste…¹ Blood…² Maxim…³ Exerc…⁴ Heart…⁵
#>    <dbl> <fct> <fct>            <dbl>      <dbl> <lgl>     <dbl> <fct>   <fct>  
#>  1    63 Male  Typical angina     145        233 TRUE        150 No      No     
#>  2    37 Male  Non-anginal pain   130        250 FALSE       187 No      No     
#>  3    56 Male  Atypical angina    120        236 FALSE       178 No      No     
#>  4    57 Male  Asymptomatic       140        192 FALSE       148 No      No     
#>  5    44 Male  Atypical angina    120        263 FALSE       173 No      No     
#>  6    52 Male  Non-anginal pain   172        199 TRUE        162 No      No     
#>  7    57 Male  Non-anginal pain   150        168 FALSE       174 No      No     
#>  8    54 Male  Asymptomatic       140        239 FALSE       160 No      No     
#>  9    49 Male  Atypical angina    130        266 FALSE       171 No      No     
#> 10    64 Male  Typical angina     110        211 FALSE       144 Yes     No     
#> # … with 82 more rows, and abbreviated variable names ¹​Cholesterol,
#> #   ²​BloodSugar, ³​MaximumHR, ⁴​ExerciseInducedAngina, ⁵​HeartDisease
#> 
#> $`Female,Yes`
#> # A tibble: 25 × 9
#>      Age Sex    ChestPain           BP Cholest…¹ Blood…² Maxim…³ Exerc…⁴ Heart…⁵
#>    <dbl> <fct>  <fct>            <dbl>     <dbl> <lgl>     <dbl> <fct>   <fct>  
#>  1    62 Female Asymptomatic       140       268 FALSE       160 No      Yes    
#>  2    65 Female Asymptomatic       150       225 FALSE       114 No      Yes    
#>  3    61 Female Asymptomatic       130       330 FALSE       169 No      Yes    
#>  4    51 Female Asymptomatic       130       305 FALSE       142 Yes     Yes    
#>  5    62 Female Asymptomatic       160       164 FALSE       145 No      Yes    
#>  6    60 Female Asymptomatic       150       258 FALSE       157 No      Yes    
#>  7    61 Female Asymptomatic       145       307 FALSE       146 Yes     Yes    
#>  8    43 Female Asymptomatic       132       341 TRUE        136 Yes     Yes    
#>  9    62 Female Non-anginal pain   130       263 FALSE        97 No      Yes    
#> 10    63 Female Asymptomatic       150       407 FALSE       154 No      Yes    
#> # … with 15 more rows, and abbreviated variable names ¹​Cholesterol,
#> #   ²​BloodSugar, ³​MaximumHR, ⁴​ExerciseInducedAngina, ⁵​HeartDisease
#> 
#> $`Male,Yes`
#> # A tibble: 114 × 9
#>      Age Sex   ChestPain           BP Choleste…¹ Blood…² Maxim…³ Exerc…⁴ Heart…⁵
#>    <dbl> <fct> <fct>            <dbl>      <dbl> <lgl>     <dbl> <fct>   <fct>  
#>  1    67 Male  Asymptomatic       160        286 FALSE       108 Yes     Yes    
#>  2    67 Male  Asymptomatic       120        229 FALSE       129 Yes     Yes    
#>  3    63 Male  Asymptomatic       130        254 FALSE       147 No      Yes    
#>  4    53 Male  Asymptomatic       140        203 TRUE        155 Yes     Yes    
#>  5    56 Male  Non-anginal pain   130        256 TRUE        142 Yes     Yes    
#>  6    48 Male  Atypical angina    110        229 FALSE       168 No      Yes    
#>  7    58 Male  Atypical angina    120        284 FALSE       160 No      Yes    
#>  8    58 Male  Non-anginal pain   132        224 FALSE       173 No      Yes    
#>  9    60 Male  Asymptomatic       130        206 FALSE       132 Yes     Yes    
#> 10    40 Male  Asymptomatic       110        167 FALSE       114 Yes     Yes    
#> # … with 104 more rows, and abbreviated variable names ¹​Cholesterol,
#> #   ²​BloodSugar, ³​MaximumHR, ⁴​ExerciseInducedAngina, ⁵​HeartDisease
#> 

#Move inward from maximum depth
heart_disease %>%
    divide(
        Sex,
        HeartDisease,
        ChestPain,
        depth = -1
    )
#> $Female
#> $Female$`No|Typical angina`
#> # A tibble: 4 × 6
#>     Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>   <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#> 1    58   150         283 TRUE             162 No                   
#> 2    66   150         226 FALSE            114 No                   
#> 3    69   140         239 FALSE            151 No                   
#> 4    60   150         240 FALSE            171 No                   
#> 
#> $Female$`No|Atypical angina`
#> # A tibble: 16 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    41   130         204 FALSE            172 No                   
#>  2    56   140         294 FALSE            153 No                   
#>  3    71   160         302 FALSE            162 No                   
#>  4    41   105         198 FALSE            168 No                   
#>  5    45   130         234 FALSE            175 No                   
#>  6    55   135         250 FALSE            161 No                   
#>  7    54   132         288 TRUE             159 Yes                  
#>  8    45   112         160 FALSE            138 No                   
#>  9    63   140         195 FALSE            179 No                   
#> 10    50   120         244 FALSE            162 No                   
#> 11    46   105         204 FALSE            172 No                   
#> 12    34   118         210 FALSE            192 No                   
#> 13    74   120         269 FALSE            121 Yes                  
#> 14    49   134         271 FALSE            162 No                   
#> 15    41   126         306 FALSE            163 No                   
#> 16    55   132         342 FALSE            166 No                   
#> 
#> $Female$`Yes|Atypical angina`
#> # A tibble: 2 × 6
#>     Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>   <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#> 1    58   136         319 TRUE             152 No                   
#> 2    57   130         236 FALSE            174 No                   
#> 
#> $Female$`No|Non-anginal pain`
#> # A tibble: 34 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    48   130         275 FALSE            139 No                   
#>  2    50   120         219 FALSE            158 No                   
#>  3    58   120         340 FALSE            172 No                   
#>  4    65   140         417 TRUE             157 No                   
#>  5    46   142         177 FALSE            160 Yes                  
#>  6    54   135         304 TRUE             170 No                   
#>  7    65   155         269 FALSE            148 No                   
#>  8    65   160         360 FALSE            151 No                   
#>  9    51   140         308 FALSE            142 No                   
#> 10    53   128         216 FALSE            115 No                   
#> # … with 24 more rows
#> 
#> $Female$`Yes|Non-anginal pain`
#> # A tibble: 1 × 6
#>     Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>   <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#> 1    62   130         263 FALSE             97 No                   
#> 
#> $Female$`No|Asymptomatic`
#> # A tibble: 18 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    57   120         354 FALSE            163 Yes                  
#>  2    53   130         264 FALSE            143 No                   
#>  3    53   138         234 FALSE            160 No                   
#>  4    57   128         303 FALSE            159 No                   
#>  5    35   138         183 FALSE            182 No                   
#>  6    62   124         209 FALSE            163 No                   
#>  7    42   102         265 FALSE            122 No                   
#>  8    58   100         248 FALSE            122 No                   
#>  9    62   140         394 FALSE            157 No                   
#> 10    45   138         236 FALSE            152 Yes                  
#> 11    50   110         254 FALSE            159 No                   
#> 12    64   180         325 FALSE            154 Yes                  
#> 13    46   138         243 FALSE            152 Yes                  
#> 14    64   130         303 FALSE            122 No                   
#> 15    49   130         269 FALSE            163 No                   
#> 16    67   106         223 FALSE            142 No                   
#> 17    71   112         149 FALSE            125 No                   
#> 18    58   130         197 FALSE            131 No                   
#> 
#> $Female$`Yes|Asymptomatic`
#> # A tibble: 22 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    62   140         268 FALSE            160 No                   
#>  2    65   150         225 FALSE            114 No                   
#>  3    61   130         330 FALSE            169 No                   
#>  4    51   130         305 FALSE            142 Yes                  
#>  5    62   160         164 FALSE            145 No                   
#>  6    60   150         258 FALSE            157 No                   
#>  7    61   145         307 FALSE            146 Yes                  
#>  8    43   132         341 TRUE             136 Yes                  
#>  9    63   150         407 FALSE            154 No                   
#> 10    56   200         288 TRUE             133 Yes                  
#> # … with 12 more rows
#> 
#> 
#> $Male
#> $Male$`No|Typical angina`
#> # A tibble: 12 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    63   145         233 TRUE             150 No                   
#>  2    64   110         211 FALSE            144 Yes                  
#>  3    40   140         199 FALSE            178 Yes                  
#>  4    51   125         213 FALSE            125 Yes                  
#>  5    34   118         182 FALSE            174 No                   
#>  6    52   118         186 FALSE            190 No                   
#>  7    52   152         298 TRUE             178 No                   
#>  8    42   148         244 FALSE            178 No                   
#>  9    59   178         270 FALSE            145 No                   
#> 10    69   160         234 TRUE             131 No                   
#> 11    56   120         193 FALSE            162 No                   
#> 12    64   170         227 FALSE            155 No                   
#> 
#> $Male$`Yes|Typical angina`
#> # A tibble: 7 × 6
#>     Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>   <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#> 1    65   138         282 TRUE             174 No                   
#> 2    59   170         288 FALSE            159 No                   
#> 3    59   160         273 FALSE            125 No                   
#> 4    38   120         231 FALSE            182 Yes                  
#> 5    61   134         234 FALSE            145 No                   
#> 6    59   134         204 FALSE            162 No                   
#> 7    45   110         264 FALSE            132 No                   
#> 
#> $Male$`No|Atypical angina`
#> # A tibble: 25 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    56   120         236 FALSE            178 No                   
#>  2    44   120         263 FALSE            173 No                   
#>  3    49   130         266 FALSE            171 No                   
#>  4    44   130         219 FALSE            188 No                   
#>  5    48   130         245 FALSE            180 No                   
#>  6    52   120         325 FALSE            172 No                   
#>  7    52   134         201 FALSE            158 No                   
#>  8    54   108         309 FALSE            156 No                   
#>  9    41   135         203 FALSE            132 No                   
#> 10    44   120         220 FALSE            170 No                   
#> # … with 15 more rows
#> 
#> $Male$`Yes|Atypical angina`
#> # A tibble: 7 × 6
#>     Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>   <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#> 1    48   110         229 FALSE            168 No                   
#> 2    58   120         284 FALSE            160 No                   
#> 3    62   120         281 FALSE            103 No                   
#> 4    66   160         246 FALSE            120 Yes                  
#> 5    54   192         283 FALSE            195 No                   
#> 6    57   124         261 FALSE            141 No                   
#> 7    57   154         232 FALSE            164 No                   
#> 
#> $Male$`No|Non-anginal pain`
#> # A tibble: 34 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    37   130         250 FALSE            187 No                   
#>  2    52   172         199 TRUE             162 No                   
#>  3    57   150         168 FALSE            174 No                   
#>  4    44   130         233 FALSE            179 Yes                  
#>  5    61   150         243 TRUE             137 Yes                  
#>  6    59   150         212 TRUE             157 No                   
#>  7    51   110         175 FALSE            123 No                   
#>  8    53   130         197 TRUE             152 No                   
#>  9    54   125         273 FALSE            152 No                   
#> 10    54   150         232 FALSE            165 No                   
#> # … with 24 more rows
#> 
#> $Male$`Yes|Non-anginal pain`
#> # A tibble: 17 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    56   130         256 TRUE             142 Yes                  
#>  2    58   132         224 FALSE            173 No                   
#>  3    64   140         335 FALSE            158 No                   
#>  4    58   112         230 FALSE            165 No                   
#>  5    50   140         233 FALSE            163 No                   
#>  6    60   140         185 FALSE            155 No                   
#>  7    46   150         231 FALSE            147 No                   
#>  8    68   180         274 TRUE             150 Yes                  
#>  9    49   120         188 FALSE            139 No                   
#> 10    57   128         229 FALSE            150 No                   
#> 11    64   125         309 FALSE            131 Yes                  
#> 12    47   108         243 FALSE            152 No                   
#> 13    70   160         269 FALSE            112 Yes                  
#> 14    69   140         254 FALSE            146 No                   
#> 15    49   118         149 FALSE            126 No                   
#> 16    59   126         218 TRUE             134 No                   
#> 17    67   152         212 FALSE            150 No                   
#> 
#> $Male$`No|Asymptomatic`
#> # A tibble: 21 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    57   140         192 FALSE            148 No                   
#>  2    54   140         239 FALSE            160 No                   
#>  3    43   150         247 FALSE            171 No                   
#>  4    59   135         234 FALSE            161 No                   
#>  5    42   140         226 FALSE            178 No                   
#>  6    65   120         177 FALSE            140 No                   
#>  7    45   104         208 FALSE            148 Yes                  
#>  8    66   120         302 FALSE            151 No                   
#>  9    48   122         222 FALSE            186 No                   
#> 10    45   115         260 FALSE            185 No                   
#> # … with 11 more rows
#> 
#> $Male$`Yes|Asymptomatic`
#> # A tibble: 83 × 6
#>      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1    67   160         286 FALSE            108 Yes                  
#>  2    67   120         229 FALSE            129 Yes                  
#>  3    63   130         254 FALSE            147 No                   
#>  4    53   140         203 TRUE             155 Yes                  
#>  5    60   130         206 FALSE            132 Yes                  
#>  6    40   110         167 FALSE            114 Yes                  
#>  7    60   117         230 TRUE             160 Yes                  
#>  8    43   120         177 FALSE            120 Yes                  
#>  9    57   150         276 FALSE            112 Yes                  
#> 10    55   132         353 FALSE            132 Yes                  
#> # … with 73 more rows
#> 
#> 

#No depth returns original data (and warning)
heart_disease %>%
    divide(
        Sex,
        depth = 0
    )
#> Warning: Split depth is 0; returning original data.
#> # A tibble: 303 × 9
#>      Age Sex    ChestPain           BP Cholest…¹ Blood…² Maxim…³ Exerc…⁴ Heart…⁵
#>    <dbl> <fct>  <fct>            <dbl>     <dbl> <lgl>     <dbl> <fct>   <fct>  
#>  1    63 Male   Typical angina     145       233 TRUE        150 No      No     
#>  2    67 Male   Asymptomatic       160       286 FALSE       108 Yes     Yes    
#>  3    67 Male   Asymptomatic       120       229 FALSE       129 Yes     Yes    
#>  4    37 Male   Non-anginal pain   130       250 FALSE       187 No      No     
#>  5    41 Female Atypical angina    130       204 FALSE       172 No      No     
#>  6    56 Male   Atypical angina    120       236 FALSE       178 No      No     
#>  7    62 Female Asymptomatic       140       268 FALSE       160 No      Yes    
#>  8    57 Female Asymptomatic       120       354 FALSE       163 Yes     No     
#>  9    63 Male   Asymptomatic       130       254 FALSE       147 No      Yes    
#> 10    53 Male   Asymptomatic       140       203 TRUE        155 Yes     Yes    
#> # … with 293 more rows, and abbreviated variable names ¹​Cholesterol,
#> #   ²​BloodSugar, ³​MaximumHR, ⁴​ExerciseInducedAngina, ⁵​HeartDisease
heart_disease %>%
    divide(
        Sex,
        HeartDisease,
        depth = -5
    )
#> Warning: Split depth is 0; returning original data.
#> # A tibble: 303 × 9
#>      Age Sex    ChestPain           BP Cholest…¹ Blood…² Maxim…³ Exerc…⁴ Heart…⁵
#>    <dbl> <fct>  <fct>            <dbl>     <dbl> <lgl>     <dbl> <fct>   <fct>  
#>  1    63 Male   Typical angina     145       233 TRUE        150 No      No     
#>  2    67 Male   Asymptomatic       160       286 FALSE       108 Yes     Yes    
#>  3    67 Male   Asymptomatic       120       229 FALSE       129 Yes     Yes    
#>  4    37 Male   Non-anginal pain   130       250 FALSE       187 No      No     
#>  5    41 Female Atypical angina    130       204 FALSE       172 No      No     
#>  6    56 Male   Atypical angina    120       236 FALSE       178 No      No     
#>  7    62 Female Asymptomatic       140       268 FALSE       160 No      Yes    
#>  8    57 Female Asymptomatic       120       354 FALSE       163 Yes     No     
#>  9    63 Male   Asymptomatic       130       254 FALSE       147 No      Yes    
#> 10    53 Male   Asymptomatic       140       203 TRUE        155 Yes     Yes    
#> # … with 293 more rows, and abbreviated variable names ¹​Cholesterol,
#> #   ²​BloodSugar, ³​MaximumHR, ⁴​ExerciseInducedAngina, ⁵​HeartDisease

#Larger than maximum depth returns maximum depth (default)
heart_disease %>%
    divide(
        Sex,
        depth = 100
    )
#> $Female
#> # A tibble: 97 × 8
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR Exerc…¹ Heart…²
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>   <fct>  
#>  1    41 Atypical angina    130         204 FALSE            172 No      No     
#>  2    62 Asymptomatic       140         268 FALSE            160 No      Yes    
#>  3    57 Asymptomatic       120         354 FALSE            163 Yes     No     
#>  4    56 Atypical angina    140         294 FALSE            153 No      No     
#>  5    48 Non-anginal pain   130         275 FALSE            139 No      No     
#>  6    58 Typical angina     150         283 TRUE             162 No      No     
#>  7    50 Non-anginal pain   120         219 FALSE            158 No      No     
#>  8    58 Non-anginal pain   120         340 FALSE            172 No      No     
#>  9    66 Typical angina     150         226 FALSE            114 No      No     
#> 10    69 Typical angina     140         239 FALSE            151 No      No     
#> # … with 87 more rows, and abbreviated variable names ¹​ExerciseInducedAngina,
#> #   ²​HeartDisease
#> 
#> $Male
#> # A tibble: 206 × 8
#>      Age ChestPain           BP Cholesterol BloodSugar MaximumHR Exerc…¹ Heart…²
#>    <dbl> <fct>            <dbl>       <dbl> <lgl>          <dbl> <fct>   <fct>  
#>  1    63 Typical angina     145         233 TRUE             150 No      No     
#>  2    67 Asymptomatic       160         286 FALSE            108 Yes     Yes    
#>  3    67 Asymptomatic       120         229 FALSE            129 Yes     Yes    
#>  4    37 Non-anginal pain   130         250 FALSE            187 No      No     
#>  5    56 Atypical angina    120         236 FALSE            178 No      No     
#>  6    63 Asymptomatic       130         254 FALSE            147 No      Yes    
#>  7    53 Asymptomatic       140         203 TRUE             155 Yes     Yes    
#>  8    57 Asymptomatic       140         192 FALSE            148 No      No     
#>  9    56 Non-anginal pain   130         256 TRUE             142 Yes     Yes    
#> 10    44 Atypical angina    120         263 FALSE            173 No      No     
#> # … with 196 more rows, and abbreviated variable names ¹​ExerciseInducedAngina,
#> #   ²​HeartDisease
#>