How to Force Merging Rows Ignoring Columns Names

Sometimes, you have multiple data frames with a same number of columns but different names,

Data Frame A

Name Month Salary
Tom Petty Jan 5000

Data Frame B

Full Name Date Wage
Susan Walsh Jan 6000

but you want to merge them together as below.

Name Month Salary
Tom Petty Jan 5000
Susan Walsh Jan 6000

The 'Merge' command you can use from the UI menu requires those data frames to have exact same names. If otherwise, you'll get an error.

One way to workaround it is to rename the column names first, then do the merge.

But for those who think that is a bit cumbersome, there is a way to do 'force merge' by writing a cutom R function.

Here's the function.

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

This is defining a function called 'force_bind' and it is setting the column names of the 'df2' data frame with the column names of the 'df1' data frame, then it's caling 'bind_rows' function, which does the 'merge'.

You can copy and paste the above function in the R Script editor and click the 'Save' button.

Then, go back to the data frame you want to do the 'mege' operation for and select 'Custom R Command' from the Step menu.

And type something like the below.

force_bind(Employee_Data_3)

The 'Employee_Data_3' is a target data frame.

Once you run this, you will get the two data frames merged with the column names from the main data frame.