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 tobind_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"
. Seevctrs::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_…¹ skin_…² eye_c…³ birth…⁴ sex gender homew…⁵
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> <chr>
#> 1 Luke … 172 77 blond fair blue 19 male mascu… Tatooi…
#> 2 C-3PO 167 75 NA gold yellow 112 none mascu… Tatooi…
#> 3 R2-D2 96 32 NA white,… red 33 none mascu… Naboo
#> 4 Darth… 202 136 none white yellow 41.9 male mascu… Tatooi…
#> 5 Biggs… 183 84 black light brown 24 male mascu… Tatooi…
#> 6 Obi-W… 182 77 auburn… fair blue-g… 57 male mascu… Stewjon
#> 7 Anaki… 188 84 blond fair blue 41.9 male mascu… Tatooi…
#> 8 Wilhu… 180 NA auburn… fair blue 64 male mascu… Eriadu
#> # … with 4 more variables: species <chr>, films <list>, vehicles <list>,
#> # starships <list>, and abbreviated variable names ¹hair_color,
#> # ²skin_color, ³eye_color, ⁴birth_year, ⁵homeworld
# The contents of lists are spliced automatically:
bind_rows(list(one, two))
#> # A tibble: 8 × 14
#> name height mass hair_…¹ skin_…² eye_c…³ birth…⁴ sex gender homew…⁵
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> <chr>
#> 1 Luke … 172 77 blond fair blue 19 male mascu… Tatooi…
#> 2 C-3PO 167 75 NA gold yellow 112 none mascu… Tatooi…
#> 3 R2-D2 96 32 NA white,… red 33 none mascu… Naboo
#> 4 Darth… 202 136 none white yellow 41.9 male mascu… Tatooi…
#> 5 Biggs… 183 84 black light brown 24 male mascu… Tatooi…
#> 6 Obi-W… 182 77 auburn… fair blue-g… 57 male mascu… Stewjon
#> 7 Anaki… 188 84 blond fair blue 41.9 male mascu… Tatooi…
#> 8 Wilhu… 180 NA auburn… fair blue 64 male mascu… Eriadu
#> # … with 4 more variables: species <chr>, films <list>, vehicles <list>,
#> # starships <list>, and abbreviated variable names ¹hair_color,
#> # ²skin_color, ³eye_color, ⁴birth_year, ⁵homeworld
bind_rows(split(starwars, starwars$homeworld))
#> # A tibble: 77 × 14
#> name height mass hair_…¹ skin_…² eye_c…³ birth…⁴ sex gender homew…⁵
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> <chr>
#> 1 Leia… 150 49 brown light brown 19 fema… femin… Aldera…
#> 2 Bail… 191 NA black tan brown 67 male mascu… Aldera…
#> 3 Raym… 188 79 brown light brown NA male mascu… Aldera…
#> 4 Ratt… 79 15 none grey, … unknown NA male mascu… Aleen …
#> 5 Lobot 175 79 none light blue 37 male mascu… Bespin
#> 6 Jek … 180 110 brown fair blue NA male mascu… Bestin…
#> 7 Nute… 191 90 none mottle… red NA male mascu… Cato N…
#> 8 Ki-A… 198 82 white pale yellow 92 male mascu… Cerea
#> 9 Mas … 196 NA none blue blue NA male mascu… Champa…
#> 10 Mon … 150 NA auburn fair blue 48 fema… femin… Chandr…
#> # … with 67 more rows, 4 more variables: species <chr>, films <list>,
#> # vehicles <list>, starships <list>, and abbreviated variable names
#> # ¹hair_color, ²skin_color, ³eye_color, ⁴birth_year, ⁵homeworld
bind_rows(list(one, two), list(two, one))
#> # A tibble: 16 × 14
#> name height mass hair_…¹ skin_…² eye_c…³ birth…⁴ sex gender homew…⁵
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> <chr>
#> 1 Luke… 172 77 blond fair blue 19 male mascu… Tatooi…
#> 2 C-3PO 167 75 NA gold yellow 112 none mascu… Tatooi…
#> 3 R2-D2 96 32 NA white,… red 33 none mascu… Naboo
#> 4 Dart… 202 136 none white yellow 41.9 male mascu… Tatooi…
#> 5 Bigg… 183 84 black light brown 24 male mascu… Tatooi…
#> 6 Obi-… 182 77 auburn… fair blue-g… 57 male mascu… Stewjon
#> 7 Anak… 188 84 blond fair blue 41.9 male mascu… Tatooi…
#> 8 Wilh… 180 NA auburn… fair blue 64 male mascu… Eriadu
#> 9 Bigg… 183 84 black light brown 24 male mascu… Tatooi…
#> 10 Obi-… 182 77 auburn… fair blue-g… 57 male mascu… Stewjon
#> 11 Anak… 188 84 blond fair blue 41.9 male mascu… Tatooi…
#> 12 Wilh… 180 NA auburn… fair blue 64 male mascu… Eriadu
#> 13 Luke… 172 77 blond fair blue 19 male mascu… Tatooi…
#> 14 C-3PO 167 75 NA gold yellow 112 none mascu… Tatooi…
#> 15 R2-D2 96 32 NA white,… red 33 none mascu… Naboo
#> 16 Dart… 202 136 none white yellow 41.9 male mascu… Tatooi…
#> # … with 4 more variables: species <chr>, films <list>, vehicles <list>,
#> # starships <list>, and abbreviated variable names ¹hair_color,
#> # ²skin_color, ³eye_color, ⁴birth_year, ⁵homeworld
# 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_…¹ skin_…² eye_c…³ birth…⁴ sex gender
#> <chr> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 1 Luke Sk… 172 77 blond fair blue 19 male mascu…
#> 2 1 C-3PO 167 75 NA gold yellow 112 none mascu…
#> 3 1 R2-D2 96 32 NA white,… red 33 none mascu…
#> 4 1 Darth V… 202 136 none white yellow 41.9 male mascu…
#> 5 2 Biggs D… 183 84 black light brown 24 male mascu…
#> 6 2 Obi-Wan… 182 77 auburn… fair blue-g… 57 male mascu…
#> 7 2 Anakin … 188 84 blond fair blue 41.9 male mascu…
#> 8 2 Wilhuff… 180 NA auburn… fair blue 64 male mascu…
#> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
#> # vehicles <list>, starships <list>, and abbreviated variable names
#> # ¹hair_color, ²skin_color, ³eye_color, ⁴birth_year
bind_rows(list(a = one, b = two), .id = "id")
#> # A tibble: 8 × 15
#> id name height mass hair_…¹ skin_…² eye_c…³ birth…⁴ sex gender
#> <chr> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 a Luke Sk… 172 77 blond fair blue 19 male mascu…
#> 2 a C-3PO 167 75 NA gold yellow 112 none mascu…
#> 3 a R2-D2 96 32 NA white,… red 33 none mascu…
#> 4 a Darth V… 202 136 none white yellow 41.9 male mascu…
#> 5 b Biggs D… 183 84 black light brown 24 male mascu…
#> 6 b Obi-Wan… 182 77 auburn… fair blue-g… 57 male mascu…
#> 7 b Anakin … 188 84 blond fair blue 41.9 male mascu…
#> 8 b Wilhuff… 180 NA auburn… fair blue 64 male mascu…
#> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
#> # vehicles <list>, starships <list>, and abbreviated variable names
#> # ¹hair_color, ²skin_color, ³eye_color, ⁴birth_year
bind_rows("group 1" = one, "group 2" = two, .id = "groups")
#> # A tibble: 8 × 15
#> groups name height mass hair_…¹ skin_…² eye_c…³ birth…⁴ sex gender
#> <chr> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 group 1 Luke … 172 77 blond fair blue 19 male mascu…
#> 2 group 1 C-3PO 167 75 NA gold yellow 112 none mascu…
#> 3 group 1 R2-D2 96 32 NA white,… red 33 none mascu…
#> 4 group 1 Darth… 202 136 none white yellow 41.9 male mascu…
#> 5 group 2 Biggs… 183 84 black light brown 24 male mascu…
#> 6 group 2 Obi-W… 182 77 auburn… fair blue-g… 57 male mascu…
#> 7 group 2 Anaki… 188 84 blond fair blue 41.9 male mascu…
#> 8 group 2 Wilhu… 180 NA auburn… fair blue 64 male mascu…
#> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
#> # vehicles <list>, starships <list>, and abbreviated variable names
#> # ¹hair_color, ²skin_color, ³eye_color, ⁴birth_year
# 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 heigh…¹ mass.…² hair_…³ skin_…⁴ eye_c…⁵ birth…⁶ sex...8 gende…⁷
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Luke Sk… 172 77 blond fair blue 19 male mascul…
#> 2 C-3PO 167 75 NA gold yellow 112 none mascul…
#> 3 R2-D2 96 32 NA white,… red 33 none mascul…
#> 4 Darth V… 202 136 none white yellow 41.9 male mascul…
#> # … with 19 more variables: 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>, and abbreviated …
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 heigh…¹ mass.…² hair_…³ skin_…⁴ eye_c…⁵ birth…⁶ sex...8 gende…⁷
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Luke Sk… 172 77 blond fair blue 19 male mascul…
#> 2 C-3PO 167 75 NA gold yellow 112 none mascul…
#> 3 R2-D2 96 32 NA white,… red 33 none mascul…
#> 4 Darth V… 202 136 none white yellow 41.9 male mascul…
#> # … with 19 more variables: 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>, and abbreviated …