Merge two matrices by row names in R
Last Updated :
15 Mar, 2024
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.
- rowbind()
- 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.
Similar Reads
Merge two dataframes with same column names
In this discussion, we will explore the process of Merging two dataframes with the same column names using Pandas. To achieve this, we'll leverage the functionality of pandas.concat(), pandas.join(), and pandas.merge() functions. These methods handle the concatenation operations along a specified ax
3 min read
Get and Set Row Names for Data Frames
In this article, we will explore various methods to Get and Set row names for Data Frames in R Programming Language. What is Data Frames?Dataframes are the 2- dimensional data structure which organizes the data into rows and columns. These data frames are commonly used for manipulation, analysis, an
5 min read
Operations on Matrices in R
Matrices in R are a bunch of values, either real or complex numbers, arranged in a group of fixed number of rows and columns. Matrices are used to depict the data in a structured and well-organized format. It is necessary to enclose the elements of a matrix in parentheses or brackets. A matrix with
8 min read
Export CSV File without Row Names in R
In this article, we will learn how to export CSV files without row names in R Programming Language. In R language we use write.csv() function to create a CSV file from the data. Syntax: write.csv(df, path) Parameters: df: dataframe objectpath: local path on your system where .csv file will be writ
1 min read
Merge Two data.table Objects in R
data.table is a package that is used for working with tabular data in R. It provides an enhanced version of "data.frames", which are the standard data structure for storing data in base R. Installation Installing "data.table" package is no different from other R packages. Its recommended to run "ins
2 min read
How to Merge DataFrames Based on Multiple Columns in R?
In this article, we will discuss how to merge dataframes based on multiple columns in R Programming Language. We can merge two  dataframes based on multiple columns  by using merge() function Syntax: merge(dataframe1, dataframe2, by.x=c('column1', 'column2'...........,'column n'), by.y=c('column1',
2 min read
Array vs Matrix in R Programming
The data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Data structures in R programming are tools for holding multiple values. The two most important data structures in R ar
3 min read
Merge Two Data Frames by common Columns in R Programming - merge() Function
merge() function in R Language is used to merge two data frames by common columns. Syntax: merge(arg1, arg2, by.x, by.y) Parameters: arg1 and arg2: Data frames to be merged by.x: Common argument of first data frame by.y: Common argument of second data frame Example 1: Python3 1== # R program to merg
1 min read
How to Loop Through Column Names in R dataframes?
In this article, we will discuss how to loop through column names in dataframe in R Programming Language. Method 1: Using sapply() Here we are using sapply() function with some functions to get column names. This function will return column names with some results Syntax: sapply(dataframe,specific f
2 min read
Combining Matrices in R
Combining matrices involves the concatenation of two or more smaller matrices, either row or column wise to form a larger matrix. It is basically a data manipulation operation where the involved matrices must be of compatible sizes to execute the operation. Matrices can be combined either horizontal
3 min read