Roll up a list of arbitrary depth with data.frame's at the leaves row-wise.

fasten(
    list,
    into = NULL,
    depth = 0
)

Arguments

list

A list with data.frame's at the leaves.

into

A character vector of resulting column names. Defaults to NULL.

depth

Depth to bind the list to. Defaults to 0.

Details

Use empty strings "" in the into argument to omit column creation when rows are binded. Use positive integers for the depth 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. The leaves of the input list should be at the same depth.

Value

A tibble::tibble or reduced list

Author

Alex Zajichek

Examples

#Make a divided data frame
list <-
  heart_disease %>%
  divide(
    Sex,
    HeartDisease,
    ChestPain
  )

#Bind without creating names
list %>% 
  fasten
#> # A tibble: 303 × 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                   
#>  5    41   130         204 FALSE            172 No                   
#>  6    56   140         294 FALSE            153 No                   
#>  7    71   160         302 FALSE            162 No                   
#>  8    41   105         198 FALSE            168 No                   
#>  9    45   130         234 FALSE            175 No                   
#> 10    55   135         250 FALSE            161 No                   
#> # … with 293 more rows

#Bind with names
list %>% 
  fasten(
    into = c("Sex", "HeartDisease", "ChestPain")
  )
#> # A tibble: 303 × 9
#>    Sex    HeartDisease ChestPain       Age    BP Chole…¹ Blood…² Maxim…³ Exerc…⁴
#>    <chr>  <chr>        <chr>         <dbl> <dbl>   <dbl> <lgl>     <dbl> <fct>  
#>  1 Female No           Typical angi…    58   150     283 TRUE        162 No     
#>  2 Female No           Typical angi…    66   150     226 FALSE       114 No     
#>  3 Female No           Typical angi…    69   140     239 FALSE       151 No     
#>  4 Female No           Typical angi…    60   150     240 FALSE       171 No     
#>  5 Female No           Atypical ang…    41   130     204 FALSE       172 No     
#>  6 Female No           Atypical ang…    56   140     294 FALSE       153 No     
#>  7 Female No           Atypical ang…    71   160     302 FALSE       162 No     
#>  8 Female No           Atypical ang…    41   105     198 FALSE       168 No     
#>  9 Female No           Atypical ang…    45   130     234 FALSE       175 No     
#> 10 Female No           Atypical ang…    55   135     250 FALSE       161 No     
#> # … with 293 more rows, and abbreviated variable names ¹​Cholesterol,
#> #   ²​BloodSugar, ³​MaximumHR, ⁴​ExerciseInducedAngina

#Only retain "Sex"
list %>%
  fasten(
    into = "Sex"
  )
#> # A tibble: 303 × 7
#>    Sex      Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAngina
#>    <chr>  <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>                
#>  1 Female    58   150         283 TRUE             162 No                   
#>  2 Female    66   150         226 FALSE            114 No                   
#>  3 Female    69   140         239 FALSE            151 No                   
#>  4 Female    60   150         240 FALSE            171 No                   
#>  5 Female    41   130         204 FALSE            172 No                   
#>  6 Female    56   140         294 FALSE            153 No                   
#>  7 Female    71   160         302 FALSE            162 No                   
#>  8 Female    41   105         198 FALSE            168 No                   
#>  9 Female    45   130         234 FALSE            175 No                   
#> 10 Female    55   135         250 FALSE            161 No                   
#> # … with 293 more rows

#Only retain "HeartDisease"
list %>%
  fasten(
    into = c("", "HeartDisease")
  )
#> # A tibble: 303 × 7
#>    HeartDisease   Age    BP Cholesterol BloodSugar MaximumHR ExerciseInducedAn…¹
#>    <chr>        <dbl> <dbl>       <dbl> <lgl>          <dbl> <fct>              
#>  1 No              58   150         283 TRUE             162 No                 
#>  2 No              66   150         226 FALSE            114 No                 
#>  3 No              69   140         239 FALSE            151 No                 
#>  4 No              60   150         240 FALSE            171 No                 
#>  5 No              41   130         204 FALSE            172 No                 
#>  6 No              56   140         294 FALSE            153 No                 
#>  7 No              71   160         302 FALSE            162 No                 
#>  8 No              41   105         198 FALSE            168 No                 
#>  9 No              45   130         234 FALSE            175 No                 
#> 10 No              55   135         250 FALSE            161 No                 
#> # … with 293 more rows, and abbreviated variable name ¹​ExerciseInducedAngina

#Bind up to Sex
list %>%
  fasten(
    into = c("HeartDisease", "ChestPain"),
    depth = 1
  )
#> $Female
#> # A tibble: 97 × 8
#>    HeartDisease ChestPain         Age    BP Cholesterol BloodS…¹ Maxim…² Exerc…³
#>    <chr>        <chr>           <dbl> <dbl>       <dbl> <lgl>      <dbl> <fct>  
#>  1 No           Typical angina     58   150         283 TRUE         162 No     
#>  2 No           Typical angina     66   150         226 FALSE        114 No     
#>  3 No           Typical angina     69   140         239 FALSE        151 No     
#>  4 No           Typical angina     60   150         240 FALSE        171 No     
#>  5 No           Atypical angina    41   130         204 FALSE        172 No     
#>  6 No           Atypical angina    56   140         294 FALSE        153 No     
#>  7 No           Atypical angina    71   160         302 FALSE        162 No     
#>  8 No           Atypical angina    41   105         198 FALSE        168 No     
#>  9 No           Atypical angina    45   130         234 FALSE        175 No     
#> 10 No           Atypical angina    55   135         250 FALSE        161 No     
#> # … with 87 more rows, and abbreviated variable names ¹​BloodSugar, ²​MaximumHR,
#> #   ³​ExerciseInducedAngina
#> 
#> $Male
#> # A tibble: 206 × 8
#>    HeartDisease ChestPain        Age    BP Cholesterol BloodSu…¹ Maxim…² Exerc…³
#>    <chr>        <chr>          <dbl> <dbl>       <dbl> <lgl>       <dbl> <fct>  
#>  1 No           Typical angina    63   145         233 TRUE          150 No     
#>  2 No           Typical angina    64   110         211 FALSE         144 Yes    
#>  3 No           Typical angina    40   140         199 FALSE         178 Yes    
#>  4 No           Typical angina    51   125         213 FALSE         125 Yes    
#>  5 No           Typical angina    34   118         182 FALSE         174 No     
#>  6 No           Typical angina    52   118         186 FALSE         190 No     
#>  7 No           Typical angina    52   152         298 TRUE          178 No     
#>  8 No           Typical angina    42   148         244 FALSE         178 No     
#>  9 No           Typical angina    59   178         270 FALSE         145 No     
#> 10 No           Typical angina    69   160         234 TRUE          131 No     
#> # … with 196 more rows, and abbreviated variable names ¹​BloodSugar, ²​MaximumHR,
#> #   ³​ExerciseInducedAngina
#> 

#Same thing, but start at the leaves
list %>%
  fasten(
    into = c("HeartDisease", "ChestPain"),
    depth = -2
  )
#> $Female
#> # A tibble: 97 × 8
#>    HeartDisease ChestPain         Age    BP Cholesterol BloodS…¹ Maxim…² Exerc…³
#>    <chr>        <chr>           <dbl> <dbl>       <dbl> <lgl>      <dbl> <fct>  
#>  1 No           Typical angina     58   150         283 TRUE         162 No     
#>  2 No           Typical angina     66   150         226 FALSE        114 No     
#>  3 No           Typical angina     69   140         239 FALSE        151 No     
#>  4 No           Typical angina     60   150         240 FALSE        171 No     
#>  5 No           Atypical angina    41   130         204 FALSE        172 No     
#>  6 No           Atypical angina    56   140         294 FALSE        153 No     
#>  7 No           Atypical angina    71   160         302 FALSE        162 No     
#>  8 No           Atypical angina    41   105         198 FALSE        168 No     
#>  9 No           Atypical angina    45   130         234 FALSE        175 No     
#> 10 No           Atypical angina    55   135         250 FALSE        161 No     
#> # … with 87 more rows, and abbreviated variable names ¹​BloodSugar, ²​MaximumHR,
#> #   ³​ExerciseInducedAngina
#> 
#> $Male
#> # A tibble: 206 × 8
#>    HeartDisease ChestPain        Age    BP Cholesterol BloodSu…¹ Maxim…² Exerc…³
#>    <chr>        <chr>          <dbl> <dbl>       <dbl> <lgl>       <dbl> <fct>  
#>  1 No           Typical angina    63   145         233 TRUE          150 No     
#>  2 No           Typical angina    64   110         211 FALSE         144 Yes    
#>  3 No           Typical angina    40   140         199 FALSE         178 Yes    
#>  4 No           Typical angina    51   125         213 FALSE         125 Yes    
#>  5 No           Typical angina    34   118         182 FALSE         174 No     
#>  6 No           Typical angina    52   118         186 FALSE         190 No     
#>  7 No           Typical angina    52   152         298 TRUE          178 No     
#>  8 No           Typical angina    42   148         244 FALSE         178 No     
#>  9 No           Typical angina    59   178         270 FALSE         145 No     
#> 10 No           Typical angina    69   160         234 TRUE          131 No     
#> # … with 196 more rows, and abbreviated variable names ¹​BloodSugar, ²​MaximumHR,
#> #   ³​ExerciseInducedAngina
#> 

#Too large of depth returns original list
list %>%
  fasten(
    depth = 100
  )
#> Warning: Desired depth equal to current depth. Returning original list.
#> $Female
#> $Female$No
#> $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$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$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
#> $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$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$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
#> $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$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$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$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
#> $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$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$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$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
#> 
#> 
#> 

#Too small of depth goes to 0
list %>%
  fasten(
    depth = -100
  )
#> # A tibble: 303 × 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                   
#>  5    41   130         204 FALSE            172 No                   
#>  6    56   140         294 FALSE            153 No                   
#>  7    71   160         302 FALSE            162 No                   
#>  8    41   105         198 FALSE            168 No                   
#>  9    45   130         234 FALSE            175 No                   
#> 10    55   135         250 FALSE            161 No                   
#> # … with 293 more rows