Merge DataFrames by Row Names in R
In this article, we are going to see how to merge Dataframe by Row Name using merge in R Programming Language.
The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly to the inner join. The dataframes are combined in order of the appearance in the input function call.
Syntax: merge(x, y, by, all)
- x, y – The input dataframes
- by – specifications of the columns used for merging. In case of merging using row names, the by attribute uses ‘row.names’ value.
- all – logical true or false.
Example 1: Merge Dataframe
The output displayed is in the order of row numbers of x dataframe followed by y dataframe row numbers.
Example 2: Merge unequal Dataframe
In case of the unequal number of row numbers in the dataframes, the dataframe with the lesser number of rows is supplied with NA values, which appear in the merged dataframe.