Matrix In R - Adding Rows And Columns To A Matrix In R

Introduction

 
R is an important programming language used heavily by statisticians. It is also used in machine learning, data science, research, and many more new fields. It is a computing environment where statistical data may be implemented.
 
In a previous article, we had learned about matrices in R. In this article, we shall learn how to add rows, columns to a matrix.
 
Let's get started now.
 
Adding Row To A Matrix
 
We use function rbind() to add the row to any existing matrix. To know rbind() function in R simply type ?rbind() or help(rbind) R studio, it will give the result as below in the image.
  1. #Method 1  
  2. ?rbind()  
  3. #Method 2  
  4. help(rbind)  
R
 
Using rbind() function
 
To show how to use rbind() function in R we shall first create and print a matrix. After that, we shall use rbind() function and then see the output of using rbind() function by printing again the previously created matrix.
 
Step 1 - Creating And Printing A Matrix in R Studio
  1. #Creating a Matrix  
  2. MatrixA <- matrix(data = 1:9, nrow = 3, ncol = 3)  
  3. #Printing Matrix  
  4. MatrixA  
Output
 
MatrixA
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
 
R 
 
Step 2 - Using rbind() function
  1. #Creating a new Matrix using rbind()  
  2. MatrixB <- rbind(MatrixA, c(10,11,12))  
  3. #Printing that Matrix  
  4. MatrixB  
Output
 
> MatrixB
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[4,] 10 11 12
 
Here, a new matrix named MatrixB has been created which is the combination of a new row with values 10, 11, and 12 in the previous matrix with the name MatrixA. It has been shown in the below image how it looks in R Studio.
 
R 
 
Step 3 - Difference Between Both Matrices
 
As we can see clearly the number of rows in MatrixA and MatrixB are different. MatrixA has 3 rows while MatrixB has 4 rows. This extra one row has been added in MatrixA using rbind() function.
 
Step 4 - Using str() function to see the difference in both Matrices
 
The str() function in R gives the structure of an object. To use it we simply provide an object as its argument. Here, we shall print the structure of MatrixA and MatrixB to see the difference as below.
  1. #structure of MatrixA  
  2. str(MatrixA)  
  3. #structure of MatrixB  
  4. str(MatrixB)  
Output
 
> str(MatrixA)
int [1:3, 1:3] 1 2 3 4 5 6 7 8 9
> str(MatrixB)
num [1:4, 1:3] 1 2 3 10 4 5 6 11 7 8 ... 
 
R 
 
Adding Column To A Matrix 
 
For adding a column to a Matrix in we use cbind() function. To know more about cbind() function simply type ?cbind() or help(cbind) in R. It will display documentation of cbind() function in R documentation as shown below.
  1. #Method 1   
  2. ?cbind()  
  3. #Method 2  
  4. help(cbind)  
R
 
Using cbind() function
 
We shall use the above matrix with the created name MatrixA. Now we shall add a new column into it using cbind() function as below. We shall give the name of this matrix MatrixC.
  1. #Creating a new Matrix using cbind()  
  2. MatrixC <- cbind(MatrixA, c(10, 11, 12))  
  3. #Printing Matrix  
  4. MatrixC  
Output
 
> MatrixC
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
 
R 
 
Here we have added a new column i.e., the 4th column with data 10, 11, and 12 using cbind() function. It has been shown in the above image and we can also print the structure of MatrixC to know this as below.
  1. #Printing structure of MatrixC  
  2. str(MatrixC)  
Output
 
num [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ... 
 
R 
 
Matrix After using rbind() And cbind() function - Conclusion
 
As above we created two matrices named MatrixB and MatrixC using two different functions rbind() and cbind() in R studio. Now we can see the structure of MatrixB and MatrixC using str() function. Both matrices have been created with MatrixA which was of the dimension 3*3 i.e., 3 rows and 3 columns. Now MatrixB has become of the dimension 4 rows and 3 columns. The number of rows became here 4 from 3 rows because we have used rbind() function to add rows and hence the data of columns and number of columns remains the same. MatrixC has been created by cbind() function hence the number of columns has become 4 from 3 while the number of rows remains the same. Its dimension has become 3*4 i.e., 3 rows and 4 columns.
  1. #Priting structure of MatrixB and MatrixC  
  2. str(MatrixB)  
  3. str(MatrixC)  
Output
 
> str(MatrixB)
num [1:4, 1:3] 1 2 3 10 4 5 6 11 7 8 ...
> str(MatrixC)
num [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ... 
 
R 
 

Summary

 
In this article, we have learned how to add row and column to a matrix in R using R studio and we saw how to print the structure of an object using str() function.
 
I hope you have learned and enjoyed reading this article. You’re welcome to like, comment, share and leave any type of suggestion regarding this information.