Open In App

Append a row to a matrix using R

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

In this article, we will examine various methods to append a row into a matrix in the R Programming Language.

What is a matrix?

A matrix is a two-dimensional data set, which is a collection of rows and columns. It contains n rows and m columns. Inside the matrix, rows are arranged horizontally and columns are arranged vertically. The data that was present inside the matrix is in the form of numbers, symbols, and characters are called the elements or data points of a matrix. By using the function ‘matrix()’ matrices are created. These are some types of matrices:

How to append a row into the matrix?

Append denotes inserting new data into the datasets. To append a row into a matrix, need to create a matrix by using the function ‘matrix()’. Later, create a new row by using a vector or a list. Then, append it by using the required functions such as ‘cbind()’ or ‘rbind()’. These functions or methods can be able to combine vectors, data frames, and matrices. Some of the methods to append a row into a matrix are

  1. rbind()
  2. cbind()

Append a row into the matrix by using rbind()

The function rbind() stands for row bind. It is one of the function used to append a row into a matrix in an efficient manner. This function can have ability to combine data sets such as matrices, data frames, and vectors.

rbind( x , values )

Here, created a matrix by using the function ‘matrix()’. After, we created a new row by using a vector ‘c()’, to insert it into the matrix by using the function ‘rbind()’.

R
print("Before inserting a row into a matrix")
m1=matrix(4:9,ncol=2)
print(m1)      
new=c(7,8)

#combining
m2=rbind(m1,new)    
print("After inserting a row into a matrix")
print(m2)

Output:

[1] "Before inserting a row into a matrix"   
[,1] [,2]
[1,] 4 7
[2,] 5 8
[3,] 6 9

[1] "After inserting a row into a matrix"
[,1] [,2]
4 7
5 8
6 9
7 8

Here, created a matrix containing characters. After, we created a new row by using a vector ‘c()’, to insert it into the matrix by using the function ‘rbind()’. The function ‘ncol()’ represents the number of columns required into the matrix.

R
print("Before inserting a row into a matrix")
a=c("a","b","c")
b=c("d","e","f")
m1=matrix(c(a,b),ncol=2)
print(m1)      #printing
new=c("g","h")

#combining
m2=rbind(m1,new)    
print("After inserting a row into a matrix")
print(m2)

Output:

[1] "Before inserting a row into a matrix"
[,1] [,2]
[1,] "a" "d"
[2,] "b" "e"
[3,] "c" "f"

[1] "After inserting a row into a matrix"
[,1] [,2]
"a" "d"
"b" "e"
"c" "f"
"g" "h"

Append a row into the matrix by using cbind()

The function ‘cbind()’ stands for column bind, can have ability to append a new row into the matrix. It is a function that can works with different data sets such as matrices, data frames, and vectors. By using the concept of transposing, a new row is inserted into the matrix.

cbind(x , values)

Here created a matrix by using function ‘matrix()’. After, Using the function ‘cbind()’ we append a row into the matrix. The function ‘t()’is used for transposing the matrix and ‘ ncol() ‘ represents, the number of columns required into the matrix.

R
print("Before inserting a row into a matrix")
a=c("a","b","c")
b=c("d","e","f")
m1=matrix(c(a,b),ncol=2)
print(m1)     
new=c("g","h")

#combining
m2=t(cbind(t(m1),new))   
print("After inserting a row into a matrix")
print(m2)

Output:

[1] "Before inserting a row into a matrix"
[,1] [,2]
[1,] "a" "d"
[2,] "b" "e"
[3,] "c" "f"

[1] "After inserting a row into a matrix"
[,1] [,2]
"a" "d"
"b" "e"
"c" "f"
"g" "h"

Here, created a matrix by using the function ‘matrix()’. After, using the function ‘cbind()’ we append a row into the matrix. The functions ‘t()’is used for transposing the matrix and ‘nrow()’ represents, the number of rows are required into the matrix.

R
print("Before inserting a row into a matrix")
#creating a matrix
m1=matrix(c(10:18),nrow=3)
print(m1)     
new=c(19, 20, 21)

#combining
m2=t(cbind(t(m1),new))   
print("After inserting a row into a matrix")
print(m2)

Output:

[1] "Before inserting a row into a matrix"
[,1] [,2] [,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18

[1] "After inserting a row into a matrix"
[,1] [,2] [,3]
10 13 16
11 14 17
12 15 18
19 20 21

Conclusion

In conclusion, we accomplished two different methods to append a row into the matrix by using the functions ‘rbind()’ and ‘cbind()’. R language provides versatile tools for data manipulation and analysis.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads