Create Matrix from Vectors in R - GeeksforGeeks

22 Apr, 2020
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
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 matrixnrow specifies the number of rows to be createdncol specifies the number of columns to be createdbyrow 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 vectorx <- c(5:16)  # defining row names and column namesrown <- c("row_1", "row_2", "row_3")coln <- c("col_1", "col_2", "col_3", "col_4")  # creating matrixm <- matrix(x, nrow = 3, byrow = TRUE,             dimnames = list(rown, coln))  # print matrixprint(m)  # print class of mclass(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 vectorsx <- c(1:5)y <- c(11:15)z <- c(21:25)  # creating matrixm <- matrix(c(x, y, z), ncol = 3)  # print matrixprint(m)  # print class of mclass(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 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 framesdeparse.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 vectorsx <- c(1:5)y <- c(11:15)z <- c(21:25)  # creating matrixm <- cbind(x, y, z)  # print matrixprint(m)  # print class of mclass(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 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 framesdeparse.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 vectorsx <- c(1:5)y <- c(11:15)z <- c(21:25)  # creating matrixn <- rbind(x, y, z)  # print matrixprint(n)  # print class of mclass(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"

Picked
R-Matrix
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
Filter data by multiple conditions in R using Dplyr
Change Color of Bars in Barchart using ggplot2 in R
Loops in R (for, while, repeat)
Printing Output of an R Program
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
K-Means Clustering in R Programming