Merge Data Frames by Row Names in R (Example)

 

In this R programming article you’ll learn how to merge data frames by row names.

The content of the post is structured as follows:

Let’s start right away!

 

Constructing Example Data

First, we need to create some data that we can use in the example later on. Consider the following two data frames in R:

data1 <- data.frame(x1 = c(5, 1, 4, 9, 1, 2),                  # Create first example data frame
                    x2 = c("A", "Y", "G", "F", "G", "Y"))
data1                                                          # Print data1 to RStudio console
#   x1 x2
# 1  5  A
# 2  1  Y
# 3  4  G
# 4  9  F
# 5  1  G
# 6  2  Y
 
data2 <- data.frame(y1 = c(3, 3, 4, 1, 2, 9),                  # Create second example data frame
                    y2 = c("a", "x", "a", "x", "a", "x"))
rownames(data2) <- 3:8
data2                                                          # Print data2 to RStudio console
#   y1 y2
# 3  3  a
# 4  3  x
# 5  4  a
# 6  1  x
# 7  2  a
# 8  9  x

Both example data frames contain two columns and six rows. Note that the row names of data1 range from 1 to 6 and the rows names of data2 range from 3 to 8, respectively.

Now, let’s merge this data!

 

Merge Data Frames by Row Names

In the following example, we will combine our two example data frames with the merge() function. The merge function provides the by argument, which usually specifies the column name based on which we want to merge our data.

And here comes the trick: If we specify the by argument to be equal to zero, then the merge function automatically uses the row names of our data as matching indicator. Let’s do this in practice:

merge(data1, data2, by = 0)                                    # Merge data according to row names

 

merged data by row names

Table 1: Merging Two Data Frames by Row Names.

 

Table 1 shows the output of our previous R code. As you can see the merge function retained all rows where the row names were available in both data sets. This is also called inner join.

That’s basically it. However, there is much more to learn about the merge function…

 

Video & Further Resources

I have recently published a video on the Statistics Globe YouTube channel, which shows the R programming codes of this tutorial. You can find the video below:

 

 

In addition to the video, you could have a look at some of the related posts on my homepage. You can find a selection of articles below:

 

Summary: In this tutorial you learned how to join two data frames by their row names in the R programming language. In case you have any further questions, let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top