How to bind multiple data frames joined by list?

Do you know how I can bind data frames of the same type that are joined together by a list?

If that sounds confusing, check out the data below -- that should clarify a bit.

So I'm trying to basically do dplyr::bind_rows() to combine every data frame $penalties together and every data frame $goals together. Does that make sense? It's a bit tricky to do a reprex here, since the data's kinda big.

I really appreciate any help!

mydata

#> [[1]]
#> [[1]]$goals
#> # A tibble: 7 x 10
#>   time  game_strength team  opposing_team goal  primary_assist
#>   <chr> <chr>         <chr> <chr>         <chr> <chr>         
#> 1 00:48 EV            WSH   BOS           Mike~ Greg Adams    
#> 2 19:09 PP            BOS   WSH           Will~ Glen Wesley   
#> 3 01:44 EV            BOS   WSH           Bob ~ Rick Middleton
#> 4 14:21 EV            WSH   BOS           Greg~ Peter Sundstr~
#> 5 09:39 EV            BOS   WSH           Bob ~ Keith Crowder 
#> 6 12:55 EV            WSH   BOS           Mike~ Rod Langway   
#> 7 17:55 EV            BOS   WSH           Cam ~ Geoff Courtna~
#> # ... with 4 more variables: secondary_assist <chr>, season <chr>,
#> #   league <chr>, game_url <chr>
#> 
#> [[1]]$penalties
#> # A tibble: 13 x 9
#>    time  team  opposing_team name  penalty_type penalty_mins season league
#>    <chr> <chr> <chr>         <chr> <chr>        <chr>        <chr>  <chr> 
#>  1 05:04 BOS   WSH           Will~ Roughing     2            1987-~ NHL   
#>  2 06:01 WSH   BOS           Mike~ Interference 2            1987-~ NHL   
#>  3 07:18 BOS   WSH           Cam ~ Charging     2            1987-~ NHL   
#>  4 10:09 BOS   WSH           Jay ~ Fighting     5            1987-~ NHL   
#>  5 10:09 WSH   BOS           Ed K~ Fighting     5            1987-~ NHL   
#>  6 13:51 BOS   WSH           Mich~ Holding      2            1987-~ NHL   
#>  7 16:36 WSH   BOS           Greg~ Roughing     2            1987-~ NHL   
#>  8 16:36 BOS   WSH           Geof~ Roughing     2            1987-~ NHL   
#>  9 17:19 BOS   WSH           Nevi~ Roughing     2            1987-~ NHL   
#> 10 17:19 WSH   BOS           Dale~ Roughing     2            1987-~ NHL   
#> 11 18:07 WSH   BOS           Lou ~ Interference 2            1987-~ NHL   
#> 12 05:46 WSH   BOS           Greg~ Holding      2            1987-~ NHL   
#> 13 18:16 WSH   BOS           Scot~ High sticki~ 5            1987-~ NHL   
#> # ... with 1 more variable: game_url <chr>
#> 
#> 
#> [[2]]
#> [[2]]$goals
#> # A tibble: 4 x 10
#>   time  game_strength team  opposing_team goal  primary_assist
#>   <chr> <chr>         <chr> <chr>         <chr> <chr>         
#> 1 05:47 EV            BUF   MNS           Scot~ Doug Smith    
#> 2 09:54 PP            BUF   MNS           John~ Mike Ramsey   
#> 3 14:15 EV            MNS   BUF           Neal~ Larry DePalma 
#> 4 01:52 EV            MNS   BUF           Bria~ Brian Lawton  
#> # ... with 4 more variables: secondary_assist <chr>, season <chr>,
#> #   league <chr>, game_url <chr>
#> 
#> [[2]]$penalties
#> # A tibble: 22 x 9
#>    time  team  opposing_team name  penalty_type penalty_mins season league
#>    <chr> <chr> <chr>         <chr> <chr>        <chr>        <chr>  <chr> 
#>  1 00:23 MNS   BUF           Bob ~ Holding      2            1987-~ NHL   
#>  2 02:09 BUF   MNS           Pier~ Holding      2            1987-~ NHL   
#>  3 09:21 MNS   BUF           Fran~ Roughing     2            1987-~ NHL   
#>  4 09:21 MNS   BUF           Bob ~ Roughing     2            1987-~ NHL   
#>  5 09:21 BUF   MNS           Doug~ Roughing     2            1987-~ NHL   
#>  6 10:52 BUF   MNS           Ed H~ Roughing     2            1987-~ NHL   
#>  7 10:52 MNS   BUF           Basi~ Roughing     2            1987-~ NHL   
#>  8 10:52 MNS   BUF           Basi~ Roughing     2            1987-~ NHL   
#>  9 11:05 BUF   MNS           Ray ~ Interference 2            1987-~ NHL   
#> 10 04:30 MNS   BUF           Ron ~ Interference 2            1987-~ NHL   
#> # ... with 12 more rows, and 1 more variable: game_url <chr>

Created on 2018-12-18 by the reprex package (v0.2.0).

Hello @eoppe1022!

The key function you're looking for is purrr::transpose(). It will "swap" the two levels in your list. The reproducible example below is rather verbose but should demonstrate what I mean.

# Using tibbles for prettier data frame printing
options(tibble.print_min = 3, tibble.print_max = 5)
mtcars <- tibble::as_tibble(mtcars)
iris <- tibble::as_tibble(iris)

mydata <- list(
  list(mtcars = mtcars[1:10,], iris = iris[1:50,]),
  list(mtcars = mtcars[11:20,], iris = iris[51:100,]),
  list(mtcars = mtcars[21:32,], iris = iris[101:150,]))
mydata
#> [[1]]
#> [[1]]$mtcars
#> # A tibble: 10 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  21.0    6.  160.  110.  3.90  2.62  16.5    0.    1.    4.    4.
#> 2  21.0    6.  160.  110.  3.90  2.88  17.0    0.    1.    4.    4.
#> 3  22.8    4.  108.   93.  3.85  2.32  18.6    1.    1.    4.    1.
#> # ... with 7 more rows
#> 
#> [[1]]$iris
#> # A tibble: 50 x 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#> 1         5.10        3.50         1.40       0.200 setosa 
#> 2         4.90        3.00         1.40       0.200 setosa 
#> 3         4.70        3.20         1.30       0.200 setosa 
#> # ... with 47 more rows
#> 
#> 
#> [[2]]
#> [[2]]$mtcars
#> # A tibble: 10 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  17.8    6.  168.  123.  3.92  3.44  18.9    1.    0.    4.    4.
#> 2  16.4    8.  276.  180.  3.07  4.07  17.4    0.    0.    3.    3.
#> 3  17.3    8.  276.  180.  3.07  3.73  17.6    0.    0.    3.    3.
#> # ... with 7 more rows
#> 
#> [[2]]$iris
#> # A tibble: 50 x 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>     
#> 1         7.00        3.20         4.70        1.40 versicolor
#> 2         6.40        3.20         4.50        1.50 versicolor
#> 3         6.90        3.10         4.90        1.50 versicolor
#> # ... with 47 more rows
#> 
#> 
#> [[3]]
#> [[3]]$mtcars
#> # A tibble: 12 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  21.5    4.  120.   97.  3.70  2.46  20.0    1.    0.    3.    1.
#> 2  15.5    8.  318.  150.  2.76  3.52  16.9    0.    0.    3.    2.
#> 3  15.2    8.  304.  150.  3.15  3.44  17.3    0.    0.    3.    2.
#> # ... with 9 more rows
#> 
#> [[3]]$iris
#> # A tibble: 50 x 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species  
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>    
#> 1         6.30        3.30         6.00        2.50 virginica
#> 2         5.80        2.70         5.10        1.90 virginica
#> 3         7.10        3.00         5.90        2.10 virginica
#> # ... with 47 more rows

mydata_transposed <- purrr::transpose(mydata)
mydata_transposed
#> $mtcars
#> $mtcars[[1]]
#> # A tibble: 10 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  21.0    6.  160.  110.  3.90  2.62  16.5    0.    1.    4.    4.
#> 2  21.0    6.  160.  110.  3.90  2.88  17.0    0.    1.    4.    4.
#> 3  22.8    4.  108.   93.  3.85  2.32  18.6    1.    1.    4.    1.
#> # ... with 7 more rows
#> 
#> $mtcars[[2]]
#> # A tibble: 10 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  17.8    6.  168.  123.  3.92  3.44  18.9    1.    0.    4.    4.
#> 2  16.4    8.  276.  180.  3.07  4.07  17.4    0.    0.    3.    3.
#> 3  17.3    8.  276.  180.  3.07  3.73  17.6    0.    0.    3.    3.
#> # ... with 7 more rows
#> 
#> $mtcars[[3]]
#> # A tibble: 12 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  21.5    4.  120.   97.  3.70  2.46  20.0    1.    0.    3.    1.
#> 2  15.5    8.  318.  150.  2.76  3.52  16.9    0.    0.    3.    2.
#> 3  15.2    8.  304.  150.  3.15  3.44  17.3    0.    0.    3.    2.
#> # ... with 9 more rows
#> 
#> 
#> $iris
#> $iris[[1]]
#> # A tibble: 50 x 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#> 1         5.10        3.50         1.40       0.200 setosa 
#> 2         4.90        3.00         1.40       0.200 setosa 
#> 3         4.70        3.20         1.30       0.200 setosa 
#> # ... with 47 more rows
#> 
#> $iris[[2]]
#> # A tibble: 50 x 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>     
#> 1         7.00        3.20         4.70        1.40 versicolor
#> 2         6.40        3.20         4.50        1.50 versicolor
#> 3         6.90        3.10         4.90        1.50 versicolor
#> # ... with 47 more rows
#> 
#> $iris[[3]]
#> # A tibble: 50 x 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species  
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>    
#> 1         6.30        3.30         6.00        2.50 virginica
#> 2         5.80        2.70         5.10        1.90 virginica
#> 3         7.10        3.00         5.90        2.10 virginica
#> # ... with 47 more rows

mydata_combined <- purrr::map(mydata_transposed, dplyr::bind_rows)
mydata_combined
#> $mtcars
#> # A tibble: 32 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  21.0    6.  160.  110.  3.90  2.62  16.5    0.    1.    4.    4.
#> 2  21.0    6.  160.  110.  3.90  2.88  17.0    0.    1.    4.    4.
#> 3  22.8    4.  108.   93.  3.85  2.32  18.6    1.    1.    4.    1.
#> # ... with 29 more rows
#> 
#> $iris
#> # A tibble: 150 x 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#> 1         5.10        3.50         1.40       0.200 setosa 
#> 2         4.90        3.00         1.40       0.200 setosa 
#> 3         4.70        3.20         1.30       0.200 setosa 
#> # ... with 147 more rows

Created on 2018-12-18 by the reprex package (v0.2.1)

6 Likes

:raised_hands::raised_hands::raised_hands:

:pray::pray::pray:

Thank you so much! Amazing answer

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.