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.
bind_rows(..., .id = NULL) bind_cols( ..., .name_repair = c("unique", "universal", "check_unique", "minimal") )
... | 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 |
.name_repair | One of |
bind_rows()
and bind_cols()
return the same type as
the first input, either a data frame, tbl_df
, or grouped_df
.
The output of bind_rows()
will contain a column if that column
appears in any of the inputs.
one <- starwars[1:4, ] two <- starwars[9:12, ] # You can supply data frames as arguments: bind_rows(one, two)#> # A tibble: 8 x 14 #> name height mass hair_color skin_color eye_color birth_year sex gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> #> 1 Luke Sk… 172 77 blond fair blue 19 male mascu… #> 2 C-3PO 167 75 NA gold yellow 112 none mascu… #> 3 R2-D2 96 32 NA white, bl… red 33 none mascu… #> 4 Darth V… 202 136 none white yellow 41.9 male mascu… #> 5 Biggs D… 183 84 black light brown 24 male mascu… #> 6 Obi-Wan… 182 77 auburn, wh… fair blue-gray 57 male mascu… #> 7 Anakin … 188 84 blond fair blue 41.9 male mascu… #> 8 Wilhuff… 180 NA auburn, gr… fair blue 64 male mascu… #> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, #> # vehicles <list>, starships <list>#> # A tibble: 8 x 14 #> name height mass hair_color skin_color eye_color birth_year sex gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> #> 1 Luke Sk… 172 77 blond fair blue 19 male mascu… #> 2 C-3PO 167 75 NA gold yellow 112 none mascu… #> 3 R2-D2 96 32 NA white, bl… red 33 none mascu… #> 4 Darth V… 202 136 none white yellow 41.9 male mascu… #> 5 Biggs D… 183 84 black light brown 24 male mascu… #> 6 Obi-Wan… 182 77 auburn, wh… fair blue-gray 57 male mascu… #> 7 Anakin … 188 84 blond fair blue 41.9 male mascu… #> 8 Wilhuff… 180 NA auburn, gr… fair blue 64 male mascu… #> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, #> # vehicles <list>, starships <list>#> # A tibble: 77 x 14 #> name height mass hair_color skin_color eye_color birth_year sex gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> #> 1 Leia O… 150 49 brown light brown 19 fema… femin… #> 2 Bail P… 191 NA black tan brown 67 male mascu… #> 3 Raymus… 188 79 brown light brown NA male mascu… #> 4 Ratts … 79 15 none grey, blue unknown NA male mascu… #> 5 Lobot 175 79 none light blue 37 male mascu… #> 6 Jek To… 180 110 brown fair blue NA male mascu… #> 7 Nute G… 191 90 none mottled gr… red NA male mascu… #> 8 Ki-Adi… 198 82 white pale yellow 92 male mascu… #> 9 Mas Am… 196 NA none blue blue NA male mascu… #> 10 Mon Mo… 150 NA auburn fair blue 48 fema… femin… #> # … with 67 more rows, and 5 more variables: homeworld <chr>, species <chr>, #> # films <list>, vehicles <list>, starships <list>#> # A tibble: 16 x 14 #> name height mass hair_color skin_color eye_color birth_year sex gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> #> 1 Luke S… 172 77 blond fair blue 19 male mascu… #> 2 C-3PO 167 75 NA gold yellow 112 none mascu… #> 3 R2-D2 96 32 NA white, bl… red 33 none mascu… #> 4 Darth … 202 136 none white yellow 41.9 male mascu… #> 5 Biggs … 183 84 black light brown 24 male mascu… #> 6 Obi-Wa… 182 77 auburn, wh… fair blue-gray 57 male mascu… #> 7 Anakin… 188 84 blond fair blue 41.9 male mascu… #> 8 Wilhuf… 180 NA auburn, gr… fair blue 64 male mascu… #> 9 Biggs … 183 84 black light brown 24 male mascu… #> 10 Obi-Wa… 182 77 auburn, wh… fair blue-gray 57 male mascu… #> 11 Anakin… 188 84 blond fair blue 41.9 male mascu… #> 12 Wilhuf… 180 NA auburn, gr… fair blue 64 male mascu… #> 13 Luke S… 172 77 blond fair blue 19 male mascu… #> 14 C-3PO 167 75 NA gold yellow 112 none mascu… #> 15 R2-D2 96 32 NA white, bl… red 33 none mascu… #> 16 Darth … 202 136 none white yellow 41.9 male mascu… #> # … with 5 more variables: 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 x 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 x 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 x 15 #> id name height mass hair_color skin_color eye_color birth_year sex #> <chr> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> #> 1 1 Luke Sky… 172 77 blond fair blue 19 male #> 2 1 C-3PO 167 75 NA gold yellow 112 none #> 3 1 R2-D2 96 32 NA white, bl… red 33 none #> 4 1 Darth Va… 202 136 none white yellow 41.9 male #> 5 2 Biggs Da… 183 84 black light brown 24 male #> 6 2 Obi-Wan … 182 77 auburn, wh… fair blue-gray 57 male #> 7 2 Anakin S… 188 84 blond fair blue 41.9 male #> 8 2 Wilhuff … 180 NA auburn, gr… fair blue 64 male #> # … with 6 more variables: gender <chr>, homeworld <chr>, species <chr>, #> # films <list>, vehicles <list>, starships <list>#> # A tibble: 8 x 15 #> id name height mass hair_color skin_color eye_color birth_year sex #> <chr> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> #> 1 a Luke Sky… 172 77 blond fair blue 19 male #> 2 a C-3PO 167 75 NA gold yellow 112 none #> 3 a R2-D2 96 32 NA white, bl… red 33 none #> 4 a Darth Va… 202 136 none white yellow 41.9 male #> 5 b Biggs Da… 183 84 black light brown 24 male #> 6 b Obi-Wan … 182 77 auburn, wh… fair blue-gray 57 male #> 7 b Anakin S… 188 84 blond fair blue 41.9 male #> 8 b Wilhuff … 180 NA auburn, gr… fair blue 64 male #> # … with 6 more variables: 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 x 15 #> groups name height mass hair_color skin_color eye_color birth_year sex #> <chr> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> #> 1 group 1 Luke Sk… 172 77 blond fair blue 19 male #> 2 group 1 C-3PO 167 75 NA gold yellow 112 none #> 3 group 1 R2-D2 96 32 NA white, bl… red 33 none #> 4 group 1 Darth V… 202 136 none white yellow 41.9 male #> 5 group 2 Biggs D… 183 84 black light brown 24 male #> 6 group 2 Obi-Wan… 182 77 auburn, w… fair blue-gray 57 male #> 7 group 2 Anakin … 188 84 blond fair blue 41.9 male #> 8 group 2 Wilhuff… 180 NA auburn, g… fair blue 64 male #> # … with 6 more variables: gender <chr>, homeworld <chr>, species <chr>, #> # films <list>, vehicles <list>, starships <list>#> # A tibble: 7 x 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 4if (FALSE) { # Rows do need to match when column-binding bind_cols(tibble(x = 1:3), tibble(y = 1:2)) # even with 0 columns bind_cols(tibble(x = 1:3), tibble()) } bind_cols(one, two)#>#> #> #> #> #> #>#> # A tibble: 4 x 28 #> name...1 height...2 mass...3 hair_color...4 skin_color...5 eye_color...6 #> <chr> <int> <dbl> <chr> <chr> <chr> #> 1 Luke Skywalker 172 77 blond fair blue #> 2 C-3PO 167 75 NA gold yellow #> 3 R2-D2 96 32 NA white, blue red #> 4 Darth Vader 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>, vehicles...27 <list>, #> # starships...28 <list>#>#> #> #> #> #> #>#> # A tibble: 4 x 28 #> name...1 height...2 mass...3 hair_color...4 skin_color...5 eye_color...6 #> <chr> <int> <dbl> <chr> <chr> <chr> #> 1 Luke Skywalker 172 77 blond fair blue #> 2 C-3PO 167 75 NA gold yellow #> 3 R2-D2 96 32 NA white, blue red #> 4 Darth Vader 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>, vehicles...27 <list>, #> # starships...28 <list>