Skip to content

This is an efficient implementation of the common pattern of do.call(rbind, dfs) or do.call(cbind, dfs) for binding many data frames into one.

Usage

bind_rows(..., .id = NULL)

bind_cols(
  ...,
  .name_repair = c("unique", "universal", "check_unique", "minimal")
)

Arguments

...

Data frames to combine.

Each argument can either be a data frame, a list that could be a data frame, or a list of data frames.

When row-binding, columns are matched by name, and any missing columns will be filled with NA.

When column-binding, rows are matched by position, so all data frames must have the same number of rows. To match by value, not position, see mutate-joins.

.id

Data frame identifier.

When .id is supplied, a new column of identifiers is created to link each row to its original data frame. The labels are taken from the named arguments to bind_rows(). When a list of data frames is supplied, the labels are taken from the names of the list. If no names are found a numeric sequence is used instead.

.name_repair

One of "unique", "universal", or "check_unique". See vctrs::vec_as_names() for the meaning of these options.

Value

bind_rows() and bind_cols() return the same type as the first input, either a data frame, tbl_df, or grouped_df.

Details

The output of bind_rows() will contain a column if that column appears in any of the inputs.

Examples

one <- starwars[1:4, ]
two <- starwars[9:12, ]

# You can supply data frames as arguments:
bind_rows(one, two)
#> # A tibble: 8 × 14
#>   name       height  mass hair_color skin_color eye_color birth_year sex  
#>   <chr>       <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#> 1 Luke Skyw…    172    77 blond      fair       blue            19   male 
#> 2 C-3PO         167    75 NA         gold       yellow         112   none 
#> 3 R2-D2          96    32 NA         white, bl… red             33   none 
#> 4 Darth Vad…    202   136 none       white      yellow          41.9 male 
#> 5 Biggs Dar…    183    84 black      light      brown           24   male 
#> 6 Obi-Wan K…    182    77 auburn, w… fair       blue-gray       57   male 
#> 7 Anakin Sk…    188    84 blond      fair       blue            41.9 male 
#> 8 Wilhuff T…    180    NA auburn, g… fair       blue            64   male 
#> # … with 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>

# The contents of lists are spliced automatically:
bind_rows(list(one, two))
#> # A tibble: 8 × 14
#>   name       height  mass hair_color skin_color eye_color birth_year sex  
#>   <chr>       <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#> 1 Luke Skyw…    172    77 blond      fair       blue            19   male 
#> 2 C-3PO         167    75 NA         gold       yellow         112   none 
#> 3 R2-D2          96    32 NA         white, bl… red             33   none 
#> 4 Darth Vad…    202   136 none       white      yellow          41.9 male 
#> 5 Biggs Dar…    183    84 black      light      brown           24   male 
#> 6 Obi-Wan K…    182    77 auburn, w… fair       blue-gray       57   male 
#> 7 Anakin Sk…    188    84 blond      fair       blue            41.9 male 
#> 8 Wilhuff T…    180    NA auburn, g… fair       blue            64   male 
#> # … with 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>
bind_rows(split(starwars, starwars$homeworld))
#> # A tibble: 77 × 14
#>    name      height  mass hair_color skin_color eye_color birth_year sex  
#>    <chr>      <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#>  1 Leia Org…    150    49 brown      light      brown             19 fema…
#>  2 Bail Pre…    191    NA black      tan        brown             67 male 
#>  3 Raymus A…    188    79 brown      light      brown             NA male 
#>  4 Ratts Ty…     79    15 none       grey, blue unknown           NA male 
#>  5 Lobot        175    79 none       light      blue              37 male 
#>  6 Jek Tono…    180   110 brown      fair       blue              NA male 
#>  7 Nute Gun…    191    90 none       mottled g… red               NA male 
#>  8 Ki-Adi-M…    198    82 white      pale       yellow            92 male 
#>  9 Mas Amed…    196    NA none       blue       blue              NA male 
#> 10 Mon Moth…    150    NA auburn     fair       blue              48 fema…
#> # … with 67 more rows, and 6 more variables: gender <chr>,
#> #   homeworld <chr>, species <chr>, films <list>, vehicles <list>,
#> #   starships <list>
bind_rows(list(one, two), list(two, one))
#> # A tibble: 16 × 14
#>    name      height  mass hair_color skin_color eye_color birth_year sex  
#>    <chr>      <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#>  1 Luke Sky…    172    77 blond      fair       blue            19   male 
#>  2 C-3PO        167    75 NA         gold       yellow         112   none 
#>  3 R2-D2         96    32 NA         white, bl… red             33   none 
#>  4 Darth Va…    202   136 none       white      yellow          41.9 male 
#>  5 Biggs Da…    183    84 black      light      brown           24   male 
#>  6 Obi-Wan …    182    77 auburn, w… fair       blue-gray       57   male 
#>  7 Anakin S…    188    84 blond      fair       blue            41.9 male 
#>  8 Wilhuff …    180    NA auburn, g… fair       blue            64   male 
#>  9 Biggs Da…    183    84 black      light      brown           24   male 
#> 10 Obi-Wan …    182    77 auburn, w… fair       blue-gray       57   male 
#> 11 Anakin S…    188    84 blond      fair       blue            41.9 male 
#> 12 Wilhuff …    180    NA auburn, g… fair       blue            64   male 
#> 13 Luke Sky…    172    77 blond      fair       blue            19   male 
#> 14 C-3PO        167    75 NA         gold       yellow         112   none 
#> 15 R2-D2         96    32 NA         white, bl… red             33   none 
#> 16 Darth Va…    202   136 none       white      yellow          41.9 male 
#> # … with 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>


# In addition to data frames, you can supply vectors. In the rows
# direction, the vectors represent rows and should have inner
# names:
bind_rows(
  c(a = 1, b = 2),
  c(a = 3, b = 4)
)
#> # A tibble: 2 × 2
#>       a     b
#>   <dbl> <dbl>
#> 1     1     2
#> 2     3     4

# You can mix vectors and data frames:
bind_rows(
  c(a = 1, b = 2),
  tibble(a = 3:4, b = 5:6),
  c(a = 7, b = 8)
)
#> # A tibble: 4 × 2
#>       a     b
#>   <dbl> <dbl>
#> 1     1     2
#> 2     3     5
#> 3     4     6
#> 4     7     8


# When you supply a column name with the `.id` argument, a new
# column is created to link each row to its original data frame
bind_rows(list(one, two), .id = "id")
#> # A tibble: 8 × 15
#>   id    name       height  mass hair_color skin_color eye_color birth_year
#>   <chr> <chr>       <int> <dbl> <chr>      <chr>      <chr>          <dbl>
#> 1 1     Luke Skyw…    172    77 blond      fair       blue            19  
#> 2 1     C-3PO         167    75 NA         gold       yellow         112  
#> 3 1     R2-D2          96    32 NA         white, bl… red             33  
#> 4 1     Darth Vad…    202   136 none       white      yellow          41.9
#> 5 2     Biggs Dar…    183    84 black      light      brown           24  
#> 6 2     Obi-Wan K…    182    77 auburn, w… fair       blue-gray       57  
#> 7 2     Anakin Sk…    188    84 blond      fair       blue            41.9
#> 8 2     Wilhuff T…    180    NA auburn, g… fair       blue            64  
#> # … with 7 more variables: sex <chr>, gender <chr>, homeworld <chr>,
#> #   species <chr>, films <list>, vehicles <list>, starships <list>
bind_rows(list(a = one, b = two), .id = "id")
#> # A tibble: 8 × 15
#>   id    name       height  mass hair_color skin_color eye_color birth_year
#>   <chr> <chr>       <int> <dbl> <chr>      <chr>      <chr>          <dbl>
#> 1 a     Luke Skyw…    172    77 blond      fair       blue            19  
#> 2 a     C-3PO         167    75 NA         gold       yellow         112  
#> 3 a     R2-D2          96    32 NA         white, bl… red             33  
#> 4 a     Darth Vad…    202   136 none       white      yellow          41.9
#> 5 b     Biggs Dar…    183    84 black      light      brown           24  
#> 6 b     Obi-Wan K…    182    77 auburn, w… fair       blue-gray       57  
#> 7 b     Anakin Sk…    188    84 blond      fair       blue            41.9
#> 8 b     Wilhuff T…    180    NA auburn, g… fair       blue            64  
#> # … with 7 more variables: sex <chr>, gender <chr>, homeworld <chr>,
#> #   species <chr>, films <list>, vehicles <list>, starships <list>
bind_rows("group 1" = one, "group 2" = two, .id = "groups")
#> # A tibble: 8 × 15
#>   groups  name     height  mass hair_color skin_color eye_color birth_year
#>   <chr>   <chr>     <int> <dbl> <chr>      <chr>      <chr>          <dbl>
#> 1 group 1 Luke Sk…    172    77 blond      fair       blue            19  
#> 2 group 1 C-3PO       167    75 NA         gold       yellow         112  
#> 3 group 1 R2-D2        96    32 NA         white, bl… red             33  
#> 4 group 1 Darth V…    202   136 none       white      yellow          41.9
#> 5 group 2 Biggs D…    183    84 black      light      brown           24  
#> 6 group 2 Obi-Wan…    182    77 auburn, w… fair       blue-gray       57  
#> 7 group 2 Anakin …    188    84 blond      fair       blue            41.9
#> 8 group 2 Wilhuff…    180    NA auburn, g… fair       blue            64  
#> # … with 7 more variables: sex <chr>, gender <chr>, homeworld <chr>,
#> #   species <chr>, films <list>, vehicles <list>, starships <list>

# Columns don't need to match when row-binding
bind_rows(tibble(x = 1:3), tibble(y = 1:4))
#> # A tibble: 7 × 2
#>       x     y
#>   <int> <int>
#> 1     1    NA
#> 2     2    NA
#> 3     3    NA
#> 4    NA     1
#> 5    NA     2
#> 6    NA     3
#> 7    NA     4

# Row sizes must be compatible when column-binding
try(bind_cols(tibble(x = 1:3), tibble(y = 1:2)))
#> Error in bind_cols(tibble(x = 1:3), tibble(y = 1:2)) : 
#>   Can't recycle `..1` (size 3) to match `..2` (size 2).

# Even with 0 columns
try(bind_cols(tibble(x = 1:3), tibble()))
#> Error in bind_cols(tibble(x = 1:3), tibble()) : 
#>   Can't recycle `..1` (size 3) to match `..2` (size 0).

bind_cols(one, two)
#> New names:
#>  `name` -> `name...1`
#>  `height` -> `height...2`
#>  `mass` -> `mass...3`
#>  `hair_color` -> `hair_color...4`
#>  `skin_color` -> `skin_color...5`
#>  `eye_color` -> `eye_color...6`
#>  `birth_year` -> `birth_year...7`
#>  `sex` -> `sex...8`
#>  `gender` -> `gender...9`
#>  `homeworld` -> `homeworld...10`
#>  `species` -> `species...11`
#>  `films` -> `films...12`
#>  `vehicles` -> `vehicles...13`
#>  `starships` -> `starships...14`
#>  `name` -> `name...15`
#>  `height` -> `height...16`
#>  `mass` -> `mass...17`
#>  `hair_color` -> `hair_color...18`
#>  `skin_color` -> `skin_color...19`
#>  `eye_color` -> `eye_color...20`
#>  `birth_year` -> `birth_year...21`
#>  `sex` -> `sex...22`
#>  `gender` -> `gender...23`
#>  `homeworld` -> `homeworld...24`
#>  `species` -> `species...25`
#>  `films` -> `films...26`
#>  `vehicles` -> `vehicles...27`
#>  `starships` -> `starships...28`
#> # A tibble: 4 × 28
#>   name...1 height...2 mass...3 hair_color...4 skin_color...5 eye_color...6
#>   <chr>         <int>    <dbl> <chr>          <chr>          <chr>        
#> 1 Luke Sk…        172       77 blond          fair           blue         
#> 2 C-3PO           167       75 NA             gold           yellow       
#> 3 R2-D2            96       32 NA             white, blue    red          
#> 4 Darth V…        202      136 none           white          yellow       
#> # … with 22 more variables: birth_year...7 <dbl>, sex...8 <chr>,
#> #   gender...9 <chr>, homeworld...10 <chr>, species...11 <chr>,
#> #   films...12 <list>, vehicles...13 <list>, starships...14 <list>,
#> #   name...15 <chr>, height...16 <int>, mass...17 <dbl>,
#> #   hair_color...18 <chr>, skin_color...19 <chr>, eye_color...20 <chr>,
#> #   birth_year...21 <dbl>, sex...22 <chr>, gender...23 <chr>,
#> #   homeworld...24 <chr>, species...25 <chr>, films...26 <list>, …
bind_cols(list(one, two))
#> New names:
#>  `name` -> `name...1`
#>  `height` -> `height...2`
#>  `mass` -> `mass...3`
#>  `hair_color` -> `hair_color...4`
#>  `skin_color` -> `skin_color...5`
#>  `eye_color` -> `eye_color...6`
#>  `birth_year` -> `birth_year...7`
#>  `sex` -> `sex...8`
#>  `gender` -> `gender...9`
#>  `homeworld` -> `homeworld...10`
#>  `species` -> `species...11`
#>  `films` -> `films...12`
#>  `vehicles` -> `vehicles...13`
#>  `starships` -> `starships...14`
#>  `name` -> `name...15`
#>  `height` -> `height...16`
#>  `mass` -> `mass...17`
#>  `hair_color` -> `hair_color...18`
#>  `skin_color` -> `skin_color...19`
#>  `eye_color` -> `eye_color...20`
#>  `birth_year` -> `birth_year...21`
#>  `sex` -> `sex...22`
#>  `gender` -> `gender...23`
#>  `homeworld` -> `homeworld...24`
#>  `species` -> `species...25`
#>  `films` -> `films...26`
#>  `vehicles` -> `vehicles...27`
#>  `starships` -> `starships...28`
#> # A tibble: 4 × 28
#>   name...1 height...2 mass...3 hair_color...4 skin_color...5 eye_color...6
#>   <chr>         <int>    <dbl> <chr>          <chr>          <chr>        
#> 1 Luke Sk…        172       77 blond          fair           blue         
#> 2 C-3PO           167       75 NA             gold           yellow       
#> 3 R2-D2            96       32 NA             white, blue    red          
#> 4 Darth V…        202      136 none           white          yellow       
#> # … with 22 more variables: birth_year...7 <dbl>, sex...8 <chr>,
#> #   gender...9 <chr>, homeworld...10 <chr>, species...11 <chr>,
#> #   films...12 <list>, vehicles...13 <list>, starships...14 <list>,
#> #   name...15 <chr>, height...16 <int>, mass...17 <dbl>,
#> #   hair_color...18 <chr>, skin_color...19 <chr>, eye_color...20 <chr>,
#> #   birth_year...21 <dbl>, sex...22 <chr>, gender...23 <chr>,
#> #   homeworld...24 <chr>, species...25 <chr>, films...26 <list>, …