Matrix from Vectors in detail
In R, a vector is a basic data structure consisting of homogeneous elements. These elements can have various data types, such as integer, double, character, logical, complex, or raw. The c() function is used to create vectors in R.
Syntax for creating a vector:
x <- c(val1, val2, …)Vectors can be transformed into matrices in R. Matrices are similar to arrays in other programming languages, like C, and they hold multiple data elements of the same type. You can create matrices using various functions in R that take vectors as input and specify dimensions for the matrix.
Functions Used to Convert a Vector to a Matrix:
matrix()cbind()rbind()array()diag()
1. Using matrix() Function: The matrix() function is used to create a matrix from a vector. You can specify the number of rows, columns, and whether the matrix should be filled row-wise or column-wise.
Syntax:
matrix(data, nrow, ncol, byrow, dimnames)Where:
datais the vector that contains the elements for the matrix.nrowspecifies the number of rows.ncolspecifies the number of columns.byrowis a logical value that determines if the matrix should be filled by row (TRUE) or column (FALSE).dimnamesspecifies the row and column names.
Example:
# Defining data in the vector
x <- c(1:12)
# Defining row and column names
rows <- c("row_1", "row_2", "row_3")
cols <- c("col_1", "col_2", "col_3", "col_4")
# Creating matrix
m <- matrix(x, nrow = 3, byrow = TRUE, dimnames = list(rows, cols))
# Printing the matrix
print(m)Output:
col_1 col_2 col_3 col_4
row_1 1 2 3 4
row_2 5 6 7 8
row_3 9 10 11 12Class of m:
class(m)Output:
[1] "matrix" "array"2. Using cbind() Function: The cbind() function is used to combine vectors by columns. It is important to ensure that the number of rows in each vector is the same to avoid errors.
Syntax:
cbind(v1, v2, v3, …, deparse.level)Where:
v1, v2, v3, …represent vectors, matrices, or data frames.deparse.levelspecifies the labeling of non-matrix arguments.
Example:
# Defining vectors
v1 <- c(1:5)
v2 <- c(6:10)
v3 <- c(11:15)
# Creating matrix by columns
m <- cbind(v1, v2, v3)
# Printing the matrix
print(m)Output:
v1 v2 v3
[1,] 1 6 11
[2,] 2 7 12
[3,] 3 8 13
[4,] 4 9 14
[5,] 5 10 153. Using rbind() Function: The rbind() function combines vectors by rows. Again, the number of columns in each vector must match.
Syntax:
rbind(v1, v2, v3, …, deparse.level)Example:
# Defining vectors
v1 <- c(1:5)
v2 <- c(6:10)
v3 <- c(11:15)
# Creating matrix by rows
m <- rbind(v1, v2, v3)
# Printing the matrix
print(m)Output:
[,1] [,2] [,3] [,4] [,5]
v1 1 2 3 4 5
v2 6 7 8 9 10
v3 11 12 13 14 154. Using array() Function: The array() function can also create matrices, but it’s more versatile and can handle multi-dimensional arrays.
Syntax:
array(data, dim)Where:
datais the input vector.dimis a vector specifying the dimensions.
Example:
# Defining vectors
v1 <- c(1:5)
v2 <- c(6:10)
v3 <- c(11:15)
# Using the array() function
m <- array(c(v1, v2, v3), dim = c(5, 3))
# Printing the matrix
print(m)Output:
[,1] [,2] [,3]
[1,] 1 6 11
[2,] 2 7 12
[3,] 3 8 13
[4,] 4 9 14
[5,] 5 10 155. Using diag() Function: The diag() function creates a matrix with the given vector placed on the diagonal.
Syntax:
diag(x, nrow, ncol)Where:
xis the vector that will be placed on the diagonal.nrowspecifies the number of rows.ncolspecifies the number of columns.
Example:
# Defining vectors
v1 <- c(1:5)
v2 <- c(6:10)
v3 <- c(11:15)
# Creating a matrix with vector elements on the diagonal
m <- diag(c(v1, v2, v3))
# Printing the matrix
print(m)Output:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0
[7,] 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0
[8,] 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0
[9,] 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0
[10,] 0 0 0 0 0 0 0 0 0 15 0 0 0 0 0
[11,] 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0
[12,] 0 0 0 0 0 0 0 0 0 0 0 22 0 0 0
[13,] 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0
[14,] 0 0 0 0 0 0 0 0 0 0 0 0 0 24 0
[15,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 25Class of m:
class(m)Output:
[1] "matrix" "array"