How to merge dataframes in R ?
In this article, we will discuss how to perform inner, outer, left, or right joins in a given dataframe in R Programming Language.
merge() function is used to merge or join two tables. With appropriate values provided to specific parameters, we can create the desired join.
Syntax: merge(df1, df2, by.df1, by.df2, all.df1, all.df2, sort = TRUE)
df1: one dataframe
df2: another dataframe
by.df1, by.df2: The names of the columns that are common to both df1 and df2.
all, all.df1, all.df2: Logical values that actually specify the type of merging happens.
An inner join also known as natural join, merges the two dataframes in one that contains the common elements of both. For this merge() function is simply given the values of two dataframes in consideration and on the basis of a common column a dataframe is generated.
merge(x = dataframe 1, y = data frame 2)
Outer Join merges all the columns of both data frames into one for all elements. For this, the dataframes in consideration along with all parameter assigned value TRUE has to be passed to merge() function.
merge(x = data frame 1, y = data frame 2, all = TRUE)
Note: It returns NA of unmatched columns
It gives the data which are matching all the rows in the first data frame with the corresponding values on the second data frame. For this along with the dataframes in consideration, all parameter has to be passed TRUE after giving reference of the left table.
merge(x = data frame 1, y = data frame 2, all.x = TRUE)
It gives the data which are matching all the rows in the second data frame with the corresponding values on the first data frame. For this merge() function should be provided with dataframes along with all parameters assigned TRUE. all parameters should have a reference to the right dataframe.
merge(x = data frame 1, y = data frame 2, all.y = TRUE)