R – Matrices in detail
A matrix is a two-dimensional structure in R that organizes data into rows and columns. Rows extend horizontally, while columns extend vertically. In R, matrices are homogeneous, meaning all elements must be of the same type (e.g., numeric, character, or logical).
Creating a Matrix in R
R provides the matrix() function to create matrices. The function requires a set of elements and the number of rows and columns to structure the matrix.
Note: By default, matrices in R are filled column-wise.
Syntax:
matrix(data, nrow, ncol, byrow, dimnames)
Parameters:
- data – Values to populate the matrix.
- nrow – Number of rows.
- ncol – Number of columns.
- byrow – If TRUE, fills the matrix row-wise; otherwise, column-wise.
- dimnames – Assigns names to rows and columns.
Example:
# Creating a 3x3 matrix
M = matrix(
c(10, 20, 30, 40, 50, 60, 70, 80, 90),
nrow = 3,
ncol = 3,
byrow = TRUE
)
# Naming rows and columns
rownames(M) = c("X", "Y", "Z")
colnames(M) = c("A", "B", "C")
cat("The 3x3 matrix:\n")
print(M)
Output:
The 3x3 matrix:
A B C
X 10 20 30
Y 40 50 60
Z 70 80 90
Creating Special Matrices in R
R allows generating specific types of matrices using different arguments within the matrix() function.
1. Constant Matrix: A matrix where all elements are filled with a single constant value.
Syntax:
# Creating a 4x4 matrix filled with 7
print(matrix(7, 4, 4))
Output:
[,1] [,2] [,3] [,4]
[1,] 7 7 7 7
[2,] 7 7 7 7
[3,] 7 7 7 7
[4,] 7 7 7 7
2. Diagonal Matrix: A diagonal matrix has nonzero values only on its main diagonal, with zeros elsewhere.
Syntax:
diag(values, m, n)
Example:
# Creating a 3x3 diagonal matrix
print(diag(c(2, 4, 6), 3, 3))
Output:
[,1] [,2] [,3]
[1,] 2 0 0
[2,] 0 4 0
[3,] 0 0 6
3. Identity Matrix: An identity matrix has ones along the diagonal and zeros elsewhere.
Syntax:
diag(values, m, n)
Example:
# Creating a 4x4 identity matrix
print(diag(1, 4, 4))
Output:
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
Matrix Properties
Matrix properties provide useful insights into the structure of a matrix, such as its dimensions, number of rows, and total number of elements.
Example:
# Creating a 2x4 matrix
M = matrix(c(3, 6, 9, 12, 15, 18, 21, 24), nrow = 2, ncol = 4, byrow = TRUE)
cat("The matrix:\n")
print(M)
cat("Dimensions of the matrix:\n")
print(dim(M))
cat("Number of rows:\n")
print(nrow(M))
cat("Number of columns:\n")
print(ncol(M))
cat("Total elements in the matrix:\n")
print(length(M))
Output:
The matrix:
[,1] [,2] [,3] [,4]
[1,] 3 6 9 12
[2,] 15 18 21 24
Dimensions of the matrix:
[1] 2 4
Number of rows:
[1] 2
Number of columns:
[1] 4
Total elements in the matrix:
[1] 8
Accessing Elements in R-Matrix
In R, you can access elements of a matrix using indexing, where the row index appears before the comma and the column index comes after. Let’s explore how to access rows, columns, and individual elements in a matrix.
Accessing Rows:
# Create a 3x3 matrix
matrix_A <- matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
nrow = 3,
ncol = 3,
byrow = TRUE
)
cat("The 3x3 matrix:\n")
print(matrix_A)
# Accessing the first two rows
cat("Accessing the first two rows:\n")
print(matrix_A[1:2, ])
Output:
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Accessing the first two rows:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Accessing Columns:
# Accessing the first two columns
cat("Accessing the first two columns:\n")
print(matrix_A[, 1:2])
Output:
Accessing the first two columns:
[,1] [,2]
[1,] 1 2
[2,] 4 5
[3,] 7 8
Accessing Specific Elements:
# Accessing individual elements
cat("Accessing element in the first row and second column:\n")
print(matrix_A[1, 2])
cat("Accessing element in the second row and third column:\n")
print(matrix_A[2, 3])
Output:
Accessing element in the first row and second column:
[1] 2
Accessing element in the second row and third column:
[1] 6
Accessing Submatrices:
# Accessing a submatrix (first two rows and first two columns)
cat("Accessing a submatrix (first two rows and columns):\n")
print(matrix_A[1:2, 1:2])
Output:
Accessing a submatrix (first two rows and columns):
[,1] [,2]
[1,] 1 2
[2,] 4 5
Modifying Elements in an R-Matrix
You can change the values of elements directly by assigning a new value.
Modifying an Element:
# Modifying the element in the third row and third column
matrix_A[3, 3] <- 30
cat("After modifying the element:\n")
print(matrix_A)
Output:
After modifying the element:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 30
Concatenating Matrices in R
You can concatenate matrices either by adding rows or columns using the rbind() and cbind() functions.
Concatenating a Row:
# Create a 3x3 matrix
matrix_A <- matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
nrow = 3,
ncol = 3,
byrow = TRUE
)
# Create a 1x3 matrix to append as a row
matrix_B <- matrix(c(10, 11, 12), nrow = 1, ncol = 3)
cat("The 3x3 matrix before row concatenation:\n")
print(matrix_A)
cat("The 1x3 matrix to add as a row:\n")
print(matrix_B)
# Concatenating a new row
matrix_C <- rbind(matrix_A, matrix_B)
cat("After concatenating the row:\n")
print(matrix_C)
Output:
The 3x3 matrix before row concatenation:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
The 1x3 matrix to add as a row:
[,1] [,2] [,3]
[1,] 10 11 12
After concatenating the row:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
Concatenating a Column:
# Create a 3x3 matrix
matrix_A <- matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
nrow = 3,
ncol = 3,
byrow = TRUE
)
# Create a 3x1 matrix to append as a column
matrix_B <- matrix(c(10, 11, 12), nrow = 3, ncol = 1)
cat("The 3x3 matrix before column concatenation:\n")
print(matrix_A)
cat("The 3x1 matrix to add as a column:\n")
print(matrix_B)
# Concatenating a new column
matrix_C <- cbind(matrix_A, matrix_B)
cat("After concatenating the column:\n")
print(matrix_C)
Output:
The 3x3 matrix before column concatenation:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
The 3x1 matrix to add as a column:
[,1]
[1,] 10
[2,] 11
[3,] 12
After concatenating the column:
[,1] [,2] [,3] [,4]
[1,] 1 2 3 10
[2,] 4 5 6 11
[3,] 7 8 9 12
Deleting Rows and Columns in R-Matrix
To delete rows or columns, you can use negative indexing.
Deleting a Row:
# Deleting the second row
matrix_A <- matrix_A[-2, ]
cat("After deleting the second row:\n")
print(matrix_A)
Output:
After deleting the second row:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 7 8 9
Deleting a Column:
# Deleting the second column
matrix_A <- matrix_A[, -2]
cat("After deleting the second column:\n")
print(matrix_A)
Output:
After deleting the second column:
[,1] [,3]
[1,] 1 3
[2,] 7 9