Closed
Description
After thinking about tidyverse/purrr#179, I wonder if we could improve the semantics of dataframeable objects in bind_rows()
by taking a vector viewpoint rather than a list viewpoint.
If we think about vectors, the current behaviour is not consistent because bind_rows()
effectively binds columns together:
col_vectors <- list(
a = c(1, 2),
b = c(3, 4)
)
row_vectors <- list(
c(a = 1, b = 3),
c(a = 2, b = 4)
)
bind_rows(row_vectors)
#> Error: cannot convert object to a data frame
bind_rows(col_vectors)
#> Source: local data frame [2 x 2]
#>
#> a b
#> (dbl) (dbl)
#> 1 1 3
#> 2 2 4
I think it'd be more intuitive to go the rbind()
and cbind()
way, and treat vectors differently depending on the direction of the binding. bind_rows()
would require vectors with inner names and bind_cols()
would require vectors with outer names.
Advantages:
- Arguably more intuitive if we think about vectors rather than lists.
- Better behaviour down the line, e.g.
map_df()
- This would be compatible with splicing the input as suggested in PR Allow any number of data frames and lists of dataframes in binding functions #992.
The latter would add a lot of flexibility. bind_cols()
and bind_rows()
would accept indistinctly data frames, named vectors, and lists of data frames and named vectors.
cc @jennybc