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.
#Method 1
?rbind()
#Method 2
help(rbind)
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
#Creating a Matrix
MatrixA <- matrix(data = 1:9, nrow = 3, ncol = 3)
#Printing Matrix
MatrixA
Output
MatrixA
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Step 2 - Using rbind() function
#Creating a new Matrix using rbind()
MatrixB <- rbind(MatrixA, c(10,11,12))
#Printing that Matrix
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.
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.
#structure of MatrixA
str(MatrixA)
#structure of MatrixB
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 ...
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.
#Method 1
?cbind()
#Method 2
help(cbind)
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.
#Creating a new Matrix using cbind()
MatrixC <- cbind(MatrixA, c(10, 11, 12))
#Printing Matrix
MatrixC
Output
> MatrixC
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
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.
#Printing structure of MatrixC
str(MatrixC)
Output
num [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
Matrix After using rbind() And cbind() function
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.
#Priting structure of MatrixB and MatrixC
str(MatrixB)
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 ...
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.