Create Matrix from Vectors in R
In R programming language, vector is a basic object which consists of homogeneous elements. The data type of vector can be integer, double, character, logical, complex or raw. A vector can be created by using c()
function.
Syntax:
x <- c(val1, val2, .....)
Vectors in R are the same as the arrays in C language which are used to hold multiple data values of the same type. Vectors can also be used to create matrices.
Matrices can be created with the help of Vectors by using pre-defined functions in R Programming Language. These functions take vectors as arguments along with several other arguments for matrix dimensions, etc.
Functions used for Matrix creation:
- matrix() function
- cbind() function
- rbind() function
matrix()
function
The available data is present in a single/multiple vector, then matrix() function can be used to create the matrix by passing the following arguments in the function.
Syntax:
matrix(data, nrow, ncol, byrow, dimnames)
where,
data is the input vector which represents the elements in the matrix
nrow specifies the number of rows to be created
ncol specifies the number of columns to be created
byrow specifies logical value. If TRUE, matrix will be filled by row. Default value is FALSE.
dimnames specifies the names of rows and columns
Example 1:
# defining data in the vector x < - c( 5 : 16 ) # defining row names and column names rown < - c( "row_1" , "row_2" , "row_3" ) coln < - c( "col_1" , "col_2" , "col_3" , "col_4" ) # creating matrix m < - matrix(x, nrow = 3 , byrow = TRUE, dimnames = list (rown, coln)) # print matrix print (m) # print class of m class (m) |
Output:
col_1 col_2 col_3 col_4 row_1 5 6 7 8 row_2 9 10 11 12 row_3 13 14 15 16 [1] "matrix"
If matrix to be created using data present in multiple vectors, then vector of multiple vectors is created and passed to matrix()
function as an argument.
Example 2:
# defining multiple vectors x < - c( 1 : 5 ) y < - c( 11 : 15 ) z < - c( 21 : 25 ) # creating matrix m < - matrix(c(x, y, z), ncol = 3 ) # print matrix print (m) # print class of m class (m) |
Output:
[, 1] [, 2] [, 3] [1, ] 1 11 21 [2, ] 2 12 22 [3, ] 3 13 23 [4, ] 4 14 24 [5, ] 5 15 25 [1] "matrix"
cbind()
function
cbind()
function in R programming is used to combine vectors, data frames or matrices by columns and number of rows of data sets should be equal otherwise, output will be incomprehensible.
Syntax:
cbind(v1, v2, v3, ....., deparse.level)
where,
v1, v2, v3, …. represent vectors, matrices or data frames
deparse.level used for constructing labels for non-matrix like arguments. 0 for no labels. Default value is 1 or 2 for labelling from the arguments names.
Example:
# defining multiple vectors x < - c( 1 : 5 ) y < - c( 11 : 15 ) z < - c( 21 : 25 ) # creating matrix m < - cbind(x, y, z) # print matrix print (m) # print class of m class (m) |
Output:
x y z [1, ] 1 11 21 [2, ] 2 12 22 [3, ] 3 13 23 [4, ] 4 14 24 [5, ] 5 15 25 [1] "matrix"
rbind()
function
rbind()
function in R programming is used to combine vectors, data frames or matrices by rows and number of columns of data sets should be equal otherwise, output will be insignificant.
Syntax:
rbind(v1, v2, v3, ....., deparse.level)
where,
v1, v2, v3, …. represent vectors, matrices or data frames
deparse.level used for constructing labels for non-matrix like arguments. 0 for no labels. Default value is 1 or 2 for labelling from the arguments names.
Example:
# defining multiple vectors x < - c( 1 : 5 ) y < - c( 11 : 15 ) z < - c( 21 : 25 ) # creating matrix n < - rbind(x, y, z) # print matrix print (n) # print class of m class (n) |
Output:
[, 1] [, 2] [, 3] [, 4] [, 5] x 1 2 3 4 5 y 11 12 13 14 15 z 21 22 23 24 25 [1] "matrix"
Please Login to comment...