Aliases: bind bind_rows bind_cols
Keywords:
### ** 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 gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Luke Sky… 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 Va… 202 136 none white yellow 41.9 male mascu… 5 Biggs Da… 183 84 black light brown 24 male mascu… 6 Obi-Wan … 182 77 auburn, w… fair blue-gray 57 male mascu… 7 Anakin S… 188 84 blond fair blue 41.9 male mascu… 8 Wilhuff … 180 NA auburn, g… fair blue 64 male mascu… # … with 5 more variables: 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 gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Luke Sky… 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 Va… 202 136 none white yellow 41.9 male mascu… 5 Biggs Da… 183 84 black light brown 24 male mascu… 6 Obi-Wan … 182 77 auburn, w… fair blue-gray 57 male mascu… 7 Anakin S… 188 84 blond fair blue 41.9 male mascu… 8 Wilhuff … 180 NA auburn, g… fair blue 64 male mascu… # … with 5 more variables: 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 gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Leia Or… 150 49 brown light brown 19 fema… femin… 2 Bail Pr… 191 NA black tan brown 67 male mascu… 3 Raymus … 188 79 brown light brown NA male mascu… 4 Ratts T… 79 15 none grey, blue unknown NA male mascu… 5 Lobot 175 79 none light blue 37 male mascu… 6 Jek Ton… 180 110 brown fair blue NA male mascu… 7 Nute Gu… 191 90 none mottled g… red NA male mascu… 8 Ki-Adi-… 198 82 white pale yellow 92 male mascu… 9 Mas Ame… 196 NA none blue blue NA male mascu… 10 Mon Mot… 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>
bind_rows(list(one, two), list(two, one))
# A tibble: 16 × 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, w… fair blue-gray 57 male mascu… 7 Anakin … 188 84 blond fair blue 41.9 male mascu… 8 Wilhuff… 180 NA auburn, g… fair blue 64 male mascu… 9 Biggs D… 183 84 black light brown 24 male mascu… 10 Obi-Wan… 182 77 auburn, w… fair blue-gray 57 male mascu… 11 Anakin … 188 84 blond fair blue 41.9 male mascu… 12 Wilhuff… 180 NA auburn, g… fair blue 64 male mascu… 13 Luke Sk… 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 V… 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 × 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 sex <chr> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> 1 1 Luke Skyw… 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 Vad… 202 136 none white yellow 41.9 male 5 2 Biggs Dar… 183 84 black light brown 24 male 6 2 Obi-Wan K… 182 77 auburn, w… fair blue-gray 57 male 7 2 Anakin Sk… 188 84 blond fair blue 41.9 male 8 2 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(list(a = one, b = two), .id = "id")
# A tibble: 8 × 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 Skyw… 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 Vad… 202 136 none white yellow 41.9 male 5 b Biggs Dar… 183 84 black light brown 24 male 6 b Obi-Wan K… 182 77 auburn, w… fair blue-gray 57 male 7 b Anakin Sk… 188 84 blond fair blue 41.9 male 8 b 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("group 1" = one, "group 2" = two, .id = "groups")
# A tibble: 8 × 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>
# 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)
# 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 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>, …
bind_cols(list(one, two))
# 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 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>, …