Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bind_rows() and ignore column names [duplicate]

Tags:

r

binding

dplyr

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
like image 642
jmich738 Avatar asked Jan 02 '23 09:01

jmich738


1 Answers

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
like image 129
Marius Avatar answered Jan 13 '23 14:01

Marius