How to Create a Matrix in R


How to Create a Matrix in R ?

Answer

To create matrices in R, you can use the matrix() function. This function allows you to create a matrix by specifying the data, the number of rows, and the number of columns.

How to Create a Matrix in r language

✐ Examples

1 Creating a 2x3 Matrix

In this example,

  1. We start by creating a vector named data with the values 1:6, which generates a sequence of numbers from 1 to 6.
  2. Next, we use the matrix() function to create a matrix named mat. The first argument to matrix() is the data vector we just created.
  3. We then specify the number of rows in the matrix using the nrow argument, setting it to 2. This means our matrix will have 2 rows.
  4. We also specify the number of columns using the ncol argument, setting it to 3. This means our matrix will have 3 columns.
  5. The matrix() function arranges the data in a column-major order by default, filling the matrix by columns. So, the resulting matrix mat will look like this:
    1 3 5
    2 4 6
  6. Finally, we use the print() function to display the matrix mat.

R Program

data <- 1:6
mat <- matrix(data, nrow = 2, ncol = 3)
print(mat)

Output

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

2 Creating a 3x3 Matrix with Row-wise Filling

In this example,

  1. We start by creating a vector named data with the values 1:9, which generates a sequence of numbers from 1 to 9.
  2. Next, we use the matrix() function to create a matrix named mat. The first argument to matrix() is the data vector we just created.
  3. We then specify the number of rows in the matrix using the nrow argument, setting it to 3. This means our matrix will have 3 rows.
  4. We also specify the number of columns using the ncol argument, setting it to 3. This means our matrix will have 3 columns.
  5. We use the byrow argument and set it to TRUE to fill the matrix by rows instead of the default column-major order. So, the resulting matrix mat will look like this:
    1 2 3
    4 5 6
    7 8 9
  6. Finally, we use the print() function to display the matrix mat.

R Program

data <- 1:9
mat <- matrix(data, nrow = 3, ncol = 3, byrow = TRUE)
print(mat)

Output

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

3 Creating a 4x2 Matrix and Naming Rows and Columns

In this example,

  1. We start by creating a vector named data with the values 1:8, which generates a sequence of numbers from 1 to 8.
  2. Next, we use the matrix() function to create a matrix named mat. The first argument to matrix() is the data vector we just created.
  3. We then specify the number of rows in the matrix using the nrow argument, setting it to 4. This means our matrix will have 4 rows.
  4. We also specify the number of columns using the ncol argument, setting it to 2. This means our matrix will have 2 columns.
  5. The matrix() function arranges the data in a column-major order by default, filling the matrix by columns. So, the resulting matrix mat will look like this:
    1 5
    2 6
    3 7
    4 8
  6. We then use the rownames() function to assign names to the rows of the matrix, setting them to c("Row1", "Row2", "Row3", "Row4").
  7. We also use the colnames() function to assign names to the columns of the matrix, setting them to c("Col1", "Col2").
  8. Finally, we use the print() function to display the matrix mat with its row and column names.

R Program

data <- 1:8
mat <- matrix(data, nrow = 4, ncol = 2)
rownames(mat) <- c("Row1", "Row2", "Row3", "Row4")
colnames(mat) <- c("Col1", "Col2")
print(mat)

Output

     Col1 Col2
Row1    1    5
Row2    2    6
Row3    3    7
Row4    4    8

Summary

In this tutorial, we learned How to Create a Matrix in R language with well detailed examples.




More R Matrices Tutorials

  1. How to Create a Matrix in R ?
  2. How to Combine Matrices by Columns in R ?
  3. How to Combine Matrices by Rows in R ?
  4. How to Create Diagonal Matrices in R ?
  5. How to Access Matrix Elements using Indexing and Slicing in R ?
  6. How to get Matrix Size in R ?
  7. How to get Number of Rows in Matrix in R ?
  8. How to get Number of Columns in Matrix in R ?
  9. How to do Matrix Addition in R ?
  10. How to do Matrix Subtraction in R ?
  11. How to do Matrix Multiplication in R ?
  12. How to do Scalar Multiplication on a Matrix in R ?
  13. How to do Scalar Division on a Matrix in R ?
  14. How to do Element-Wise Operations in a Matrix in R ?
  15. How to Assign Row and Column Names in a Matrix in R ?
  16. How to get Row Names in a Matrix in R ?
  17. How to get Column Names in a Matrix in R ?
  18. How to find Transpose of a Matrix in R ?
  19. How to Extract the Diagonal of a Matrix in R ?
  20. How to find Determinant of a Matrix in R ?
  21. How to find Inverse of a Matrix in R ?
  22. How to find Rank of a Matrix in R ?
  23. How to Find Eigenvalues and Eigenvectors of a Matrix in R ?
  24. How to Perform Singular Value Decomposition (SVD) in R ?
  25. How to Perform QR Decomposition in R ?
  26. How to Perform Cholesky Decomposition in R ?
  27. How to Reshape Matrices in R ?
  28. How to Convert Data Frames to Matrices in R ?
  29. How to Create Identity Matrix in R ?
  30. How to Create Zero Matrix in R ?
  31. How to Create Ones Matrix in R ?
  32. How to Generate Random Matrices in R ?
  33. How to Calculate Row and Column Sums in R ?
  34. How to Calculate Row and Column Means in R ?
  35. How to Find Row and Column Max/Min in R ?
  36. How to Perform Element-Wise Comparisons in R ?