How to Concatenate Matrices in R


A matrix is an object that has a specific number of rows and columns.

Often you may want to concatenate multiple matrices together in R.

There are two common ways to do so:

Method 1: Concatenate Matrices in R Using rbind()

new_matrix <- rbind(matrix1, matrix2)

This particular example concatenates the matrices named matrix1 and matrix2 by rows, which creates one “long” matrix.

Note that the rbind() function in R is specifically designed to “row-bind” together objects.

Also note that in order for this function to work the two matrices should have the same number of columns.

Method 2: Concatenate Matrices in R Using cbind()

new_matrix <- cbind(matrix1, matrix2)

This particular example concatenates the matrices named matrix1 and matrix2 by columns, which creates one “wide” matrix.

Note that the cbind() function in R is specifically designed to “column-bind” together objects.

Also note that in order for this function to work the two matrices should have the same number of rows.

The following examples show how to use each of these methods in practice.

Related: The Difference Between rbind and cbind in R

Example 1: Concatenate Matrices in R Using rbind()

Suppose that we create the following two matrices in R named matrix1 and matrix2:

#create first matrix
matrix1 <- matrix(1:18, nrow=6)

#view first matrix
matrix1

     [,1] [,2] [,3]
[1,]    1    7   13
[2,]    2    8   14
[3,]    3    9   15
[4,]    4   10   16
[5,]    5   11   17
[6,]    6   12   18

#create second matrix
matrix2 <- matrix(20:31, nrow=4)

#view second matrix
matrix2

     [,1] [,2] [,3]
[1,]   20   24   28
[2,]   21   25   29
[3,]   22   26   30
[4,]   23   27   31

Suppose that we would like to concatenate these two matrices together using the rbind() function.

We can use the following syntax to do so:

#concatenate matrices by rows
new_matrix <- rbind(matrix1, matrix2)

#view concatenated matrix
new_matrix

      [,1] [,2] [,3]
 [1,]    1    7   13
 [2,]    2    8   14
 [3,]    3    9   15
 [4,]    4   10   16
 [5,]    5   11   17
 [6,]    6   12   18
 [7,]   20   24   28
 [8,]   21   25   29
 [9,]   22   26   30
[10,]   23   27   31

This returns a new matrix named new_matrix that contains 10 rows and 3 columns.

One way to think of using the rbind() function is that it stacks the matrices on top of each other to create one long matrix.

Example 2: Concatenate Matrices in R Using cbind()

Suppose that we create the following two matrices in R named matrix1 and matrix2:

#create first matrix
matrix1 <- matrix(1:18, nrow=6)

#view first matrix
matrix1

     [,1] [,2] [,3]
[1,]    1    7   13
[2,]    2    8   14
[3,]    3    9   15
[4,]    4   10   16
[5,]    5   11   17
[6,]    6   12   18

#create second matrix
matrix2 <- matrix(20:31, nrow=6)

#view second matrix
matrix2

     [,1] [,2]
[1,]   20   26
[2,]   21   27
[3,]   22   28
[4,]   23   29
[5,]   24   30
[6,]   25   31

Suppose that we would like to concatenate these two matrices together using the cbind() function.

We can use the following syntax to do so:

#concatenate matrices by columns
new_matrix <- cbind(matrix1, matrix2)

#view concatenated matrix
new_matrix

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    7   13   20   26
[2,]    2    8   14   21   27
[3,]    3    9   15   22   28
[4,]    4   10   16   23   29
[5,]    5   11   17   24   30
[6,]    6   12   18   25   31

This returns a new matrix named new_matrix that contains 6 rows and 5 columns.

Notice that this produces a “wide” matrix as opposed to the rbind() method that produces a “long” matrix.

Note that both matrices had the same number of rows (6) which is why we were able to use the cbind() function to column-bind together the matrices without any errors.

Note: In each of these examples we concatenated together two matrices but we can use similar syntax to concatenate together as many matrices as we would like.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Sort a Matrix in R
How to Remove NA from Matrix in R
How to Convert Data Frame to Matrix in R
How to Convert a Table to a Matrix in R

Leave a Reply

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