Easy Tutorial
❮ R Charts Cn R Func Mean ❯

R provides a matrix type for linear algebra research, which is similar to two-dimensional arrays in other languages but offers language-level matrix operation support.

Elements in a matrix can be numbers, symbols, or mathematical expressions.

An M x N matrix is a rectangular array arranged in M (rows) and N (columns).

Here is a 2-row, 3-column matrix composed of 6 numeric elements:

In R, matrices can be created using the matrix() function with the following syntax:

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)

Parameter descriptions:

Creating a numeric matrix:

Example

# Elements arranged by row if byrow is TRUE
M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
print(M)

# Elements arranged by column if byrow is FALSE
N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
print(N)

# Define row and column names
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")

P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(P)

Executing the above code outputs:

[,1] [,2] [,3]
[1,]    3    4    5
[2,]    6    7    8
[3,]    9   10   11
[4,]   12   13   14
     [,1] [,2] [,3]
[1,]    3    7   11
[2,]    4    8   12
[3,]    5    9   13
[4,]    6   10   14
     col1 col2 col3
row1    3    4    5
row2    6    7    8
row3    9   10   11
row4   12   13   14

Transpose Matrix

R provides the t() function to transpose a matrix, swapping its rows and columns.

For example, a matrix with m rows and n columns becomes a matrix with n rows and m columns after using t().

Example

# Create a 2-row, 3-column matrix
M = matrix( c(2,6,5,1,10,4), nrow = 2, ncol = 3, byrow = TRUE)
print(M)
     [,1] [,2] [,3]
[1,]    2    6    5
[2,]    1   10    4
# Convert to a 3-row, 2-column matrix
print(t(M))

Executing the above code outputs:

[,1] [,2] [,3]
[1,]    2    6    5
[2,]    1   10    4
[1] "-----Transpose-----"
     [,1] [,2]
[1,]    2    1
[2,]    6   10
[3,]    5    4

Accessing Matrix Elements

To access elements of a matrix, use the row and column indices, similar to coordinates.

Example

# Define row and column names
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")

# Create matrix
P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(P)
# Get element at first row, third column
print(P[1,3])

# Get element at fourth row, second column
print(P[4,2])

# Get the second row
print(P[2,])

# Get the third column
print(P[,3])

Executing the above code outputs:

col1 col2 col3
row1    3    4    5
row2    6    7    8
row3    9   10   11
row4   12   13   14
[1] 5
[1] 13
col1 col2 col3 
    6    7    8 
row1 row2 row3 row4 
    5    8   11   14

Matrix Calculations

Matrices of the same size (same number of rows and columns) can be added or subtracted by performing addition or subtraction on the elements in each position. Matrix multiplication is more complex. Two matrices can be multiplied if and only if the number of columns in the first matrix equals the number of rows in the second matrix.

Matrix Addition and Subtraction

Example

# Create a 2x3 matrix
matrix1 <- matrix(c(7, 9, -1, 4, 2, 3), nrow = 2)
print(matrix1)

matrix2 <- matrix(c(6, 1, 0, 9, 3, 2), nrow = 2)
print(matrix2)

# Add the two matrices
result <- matrix1 + matrix2
cat("Addition result:","\n")
print(result)

# Subtract the two matrices
result <- matrix1 - matrix2
cat("Subtraction result:","\n")
print(result)

Executing the above code outputs:

[,1] [,2] [,3]
[1,]    7   -1    2
[2,]    9    4    3
     [,1] [,2] [,3]
[1,]    6    0    3
[2,]    1    9    2
Addition result: 
     [,1] [,2] [,3]
[1,]   13   -1    5
[2,]   10   13    5
Subtraction result: 
     [,1] [,2] [,3]
[1,]    1   -1   -1
[2,]    8   -5    1

Matrix Multiplication and Division

Example

# Create a 2x3 matrix
matrix1 <- matrix(c(7, 9, -1, 4, 2, 3), nrow = 2)
print(matrix1)

matrix2 <- matrix(c(6, 1, 0, 9, 3, 2), nrow = 2)
print(matrix2)

# Multiply the two matrices
result <- matrix1 * matrix2
cat("Multiplication result:","\n")
print(result)

# Divide the two matrices
result <- matrix1 / matrix2
cat("Division result:","\n")
print(result)

Executing the above code outputs:

[,1] [,2] [,3]
[1,]    7   -1    2
[2,]    9    4    3
     [,1] [,2] [,3]
[1,]    6    0    3
[2,]    1    9    2
Multiplication result: 
     [,1] [,2] [,3]
[1,]   42    0    6
[2,]    9   36    6
Division result: 
         [,1]      [,2]      [,3]
[1,] 1.166667      -Inf 0.6666667
[2,] 9.000000 0.4444444 1.5000000
❮ R Charts Cn R Func Mean ❯