![]() |
VOOZH | about |
In this article, we will examine various methods to append a row into a matrix in the R Programming Language.
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:
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
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()'.
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.
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"
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.
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.
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
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.