merge 3 data.frames by column names
You can use the Reduce
function to merge multiple data frames:
df_list <- list(DF1, DF2, DF3)
Reduce(function(x, y) merge(x, y, all=TRUE), df_list, accumulate=FALSE)
Or merge_recurse
from the reshape
package:
library(reshape)
data <- merge_recurse(df_list)
See also the R Wiki: Merge data frames
After researching this very same question for a couple hours today, I came up with this simple but elegant solution using a combination of 'dplyr' pipes and the base R 'merge()' function.
MergedDF <- merge(DF1, DF2) %>%
merge(DF3)
As you mention in your post, this assumes that the column names are the same and that there's the same number of rows in each data frame you are merging. This will also automatically eliminate any duplicate columns (i.e., identifiers) that were used in the merging process.