In this article, we will discuss R merge by rownames. Merging by rownames is usually needed when working with R, so let’s read this article to get more knowledge.
R merge by rownames
Luckily, many functions can help you with R merge by rownames, which consists of built-in functions or imported ones. However, we will show you two built-in functions as follows:
Using transpose, intersect() and rownames()
To perform R merge by rownames, you can call those functions. But before we approach the solution, please look at its syntax first:
- x: the data frame or matrix.
- y: the (second) data frame or matrix.
Suppose we have two data frames stored in df1, df2.
# Create first data frame df1 <- data.frame(Days=c(31, 30, 31, 30, 31)) # Set the names for each row rownames(df1) <- c("Jan","Feb","Mar","Apr","May") # Display first data frame df1 # Create second data frame df2 <- data.frame(Special=c("20/10", "20/11", "25/12", "22/1", NA)) # Set the names for each row rownames(df2) <- c("Oct", "Nov", "Dec", "Jan", "Feb") # Display second data frame df2
> df1 Days Jan 31 Feb 30 Mar 31 Apr 30 May 31 > df2 Special Oct 20/10 Nov 20/11 Dec 25/12 Jan 22/1 Feb <NA>
Now you will merge df1 and df2 by rownames:
# Select rows in df1 that have same rownames in df1 and df2 col1 <- t(t(df1[intersect(rownames(df2),rownames(df1)),])) # Select rows in df2 that have same rownames in df1 and df2 col2 <- t(t(df2[intersect(rownames(df2),rownames(df1)),])) # Create a column of the same rownames in df1 and df2 colnames <- t(t(intersect(rownames(df2),rownames(df1)))) # Merge by rownames merged <- data.frame(Days=col1,Special=col2) # Set the rownames for the merged data frame rownames(merged) <- colnames # Display the merged data frame merged
Days Special Jan 31 22/1 Feb 30 <NA>
The logic behind this approach is somehow easy. Because you want to merge df1 and df2 by row names, you use function intersect() to select the row names that they are in common with the rownames() function; after you have them, you will get the value of those rows in our df1 and df2. After that, use double t() functions to transpose the results into column representations. Finally, we merge those columns and set the row names for the merged table.
If you don’t understand the previous solution, you can use merge() to do the task instead. By looking at the function’s syntax here, you can learn about the function. We are going to use the same data frames of the first approach to make an example for this one:
# Merge by rownames merged = merge(df1,df2,by=0) #Display merged data frame merged
Row.names Days Special 1 Feb 30 <NA> 2 Jan 31 22/1
Do you see any differences between the two approaches? This merge() function is simple to use, you need to call the function and pass two data frames into it, and the argument by = 0 indicates that we want to merge them by row names. If you forgot the third argument, the result would look like this:
Days Special 1 31 20/10 2 30 20/10 3 31 20/10 4 30 20/10 5 31 20/10 6 31 20/11 7 30 20/11 8 31 20/11 9 30 20/11 10 31 20/11 11 31 25/12 12 30 25/12 13 31 25/12 14 30 25/12 15 31 25/12 16 31 22/1 17 30 22/1 18 31 22/1 19 30 22/1 20 31 22/1 21 31 <NA> 22 30 <NA> 23 31 <NA> 24 30 <NA> 25 31 <NA>
We have learned how to do R merge by rownames. It would be helpful to consider that the second approach is the quickest. In addition, you can learn more about R here. Feel free to leave a reply to us in the comments below.
Maybe you are interested:
Name of the university: HCMUT