5

This question probably has been answered before, but I can't seem to find the answer. How do you use bind_rows() to just union the two tables and ignore the column names.
The documentation on bind_rows() has the following example:

#Columns don't need to match when row-binding
bind_rows(data.frame(x = 1:3), data.frame(y = 1:4))

This returns column x and y. How do I just get a single column back without having to change the column names?
Desired output, I don't really care what the column name ends up being:

  x
1 1
2 2
3 3
4 1
5 2
6 3
7 4
jmich738
  • 1,565
  • 3
  • 24
  • 41
  • 3
    You can't `bind_rows` ignoring column names. `?bind_rows` documentation says, `"When row-binding, columns are matched by name, and any missing columns with be filled with NA."`. Using `bind_rows(unname(df1), unname(df2))` it gives an error `Error in bind_rows_(x, .id) : Argument 1 must have names`. – Ronak Shah May 16 '18 at 02:08

2 Answers2

10

You can do this with a quick 2-line function:

force_bind = function(df1, df2) {
    colnames(df2) = colnames(df1)
    bind_rows(df1, df2)
}
force_bind(df1, df2)

Output:

  x
1 1
2 2
3 3
4 1
5 2
6 3
7 4
Marius
  • 58,213
  • 16
  • 107
  • 105
0

I think we still need change the names here

bind_rows(data.frame(x = 1:3), setNames(rev(data.frame(y = 1:4)), names(data.frame(x = 1:3))))
  x
1 1
2 2
3 3
4 1
5 2
6 3
7 4
BENY
  • 317,841
  • 20
  • 164
  • 234