Open In App

Merge two matrices by row names in R

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will examine various methods to merge two matrices by row names in the R programming language.

What is a matrix?

A matrix is defined as it is a two-dimensional data set which is the collection of rows and columns. A matrix can have the ability to contain or accept data of the same type such as integers, characters, floating points, and logical into a fixed number of rows and columns. These matrixes are very similar to the vectors but differ in their dimensionalities. Using the function matrix() can create the matrix.

How do we merge the matrices using row names?

To merge matrices using row names is an easy task, by using the in-built functions in R. These in-built functions can have the ability to merge the matrices of row-wise or column-wise, by using their names. By default, row names or indexes start from 1. Some of the methods to merge the matrices are.

  1. rowbind()
  2. merge()

Using the function rowbind() to merge the matrices by row names

rowbind() is the function to merge the matrices horizontally. This function has the ability to perform operations on matrices, data frames and vectors by rows.

Syntax:

rbind(data1, data2)

Here, matrix() is the function for creating the matrices. After, using the function rbind() we merged the rows and ‘m1[1]’, ‘m1[2]’, ‘m2[1]’, ‘m2[2]’ for accessing particular rows in the matrixes and ‘nrow’ represents number of rows required in a matrix.

R
print("The 1st matrix is")
m1=matrix(1:6,nrow=2)       
print(m1)

print("The 2nd matrix is")       
m2=matrix(7:12,nrow=2)          
print(m2)

print("After combining the matrices by using rows")
res=rbind(m1[1,], m1[2,], m2[1,], m2[2,])      
print(res)

Output:

[1] "The 1st matrix is"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6

[1] "The 2nd matrix is"
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12

[1] "After combining the matrices by using rows"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[3,] 7 9 11
[4,] 8 10 12

Here, matrix() is the function for creating the matrices. After, using the function rbind() we merged the rows and ‘m1[1]’, ‘m1[2]’, ‘m2[1]’, ‘m2[2]’ for accessing particular rows in the matrixes and ‘nrow’, ‘ncol’ represents number of rows and columns are required in a matrix.

R
print("The 1st matrix is")
mat1=matrix(c("a","b","c","d"),nrow=2,ncol=2)
print(mat1)

print("The 2nd matrix is")
mat2=matrix(c("e","f","g","h"),nrow=2,ncol=2)
print(mat2)

print("After combining the matrices by using rows")
res=rbind(mat1[1,], mat2[2,], mat2[1,], mat1[2,])
print(res)

Output:

[1] "The 1st matrix is"
[,1] [,2]
[1,] "a" "c"
[2,] "b" "d"

[1] "The 2nd matrix is"
[,1] [,2]
[1,] "e" "g"
[2,] "f" "h"

[1] "After combining the matrices by using rows"
[,1] [,2]
[1,] "a" "c"
[2,] "f" "h"
[3,] "e" "g"
[4,] "b" "d"

Using the function ‘merge()’ to merge the matrices by row names

merge() is the function used for efficiently merging the matrices. The function merge() works very similarly to the function rbind() and can perform operations on matrices, dataframes and vectors.

Syntax:

merge(data1, data2)

Here, we created the matrices. After, Using the function merge() the two matrices are merged by row names. The word ‘all=TRUE’ represents selecting all elements in the matrices and ‘nrow’ represents a number of rows required in a matrix.

R
print("1st matrix is")
mat1 <- matrix(1:6,nrow = 2)
print(mat1)

print("2nd matrix is" )
mat2 <- matrix(7:12,nrow = 2)
print(mat2)

print("After merging the matrix")
df=merge(mat1,mat2,by="row.names",all="TRUE") 
print(df)

Output:

[1] "1st matrix is"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6

[1] "2nd matrix is"
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12

[1] "After merging the matrix"
Row.names V1.x V2.x V3.x V1.y V2.y V3.y
1 1 1 3 5 7 9 11
2 2 2 4 6 8 10 12

Here, we created the matrices. After, Using the function merge() the two matrices are merged by rownames. The word ‘all=TRUE’ represents selecting all elements in the matrices and ‘nrow’ represents a number of rows required in a matrix.

R
print("The 1st matrix is")
m1=matrix(c("a","b","c","d"),nrow=2)
print(m1)

print("The 2nd matrix is")
m2=matrix(c("e","f","g","h"),nrow=2)
print(m2)

print("After combining the matrices by using rows")
res=merge(m1,m2,by="row.names",all="TRUE")
print(res)

Output:

[1] "The 1st matrix is"
[,1] [,2]
[1,] "a" "c"
[2,] "b" "d"

[1] "The 2nd matrix is"
[,1] [,2]
[1,] "e" "g"
[2,] "f" "h"

[1] "After combining the matrices by using rows"
Row.names V1.x V2.x V1.y V2.y
1 1 a c e g
2 2 b d f h

Conclusion

In conclusion, we accomplished two different methods to merge the two matrices by row names using the functions ‘rbind()’ and ‘merge()’. R language provides versatile tools for analyzing and manipulation of data.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads