How to Create a Matrix in R - Step by Step Examples
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.
✐ Examples
1 Creating a 2x3 Matrix
In this example,
- We start by creating a vector named
data
with the values1:6
, which generates a sequence of numbers from 1 to 6. - Next, we use the
matrix()
function to create a matrix namedmat
. The first argument tomatrix()
is thedata
vector we just created. - 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. - We also specify the number of columns using the
ncol
argument, setting it to 3. This means our matrix will have 3 columns. - The
matrix()
function arranges the data in a column-major order by default, filling the matrix by columns. So, the resulting matrixmat
will look like this:1 3 5
2 4 6
- Finally, we use the
print()
function to display the matrixmat
.
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,
- We start by creating a vector named
data
with the values1:9
, which generates a sequence of numbers from 1 to 9. - Next, we use the
matrix()
function to create a matrix namedmat
. The first argument tomatrix()
is thedata
vector we just created. - 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. - We also specify the number of columns using the
ncol
argument, setting it to 3. This means our matrix will have 3 columns. - We use the
byrow
argument and set it toTRUE
to fill the matrix by rows instead of the default column-major order. So, the resulting matrixmat
will look like this:1 2 3
4 5 6
7 8 9
- Finally, we use the
print()
function to display the matrixmat
.
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,
- We start by creating a vector named
data
with the values1:8
, which generates a sequence of numbers from 1 to 8. - Next, we use the
matrix()
function to create a matrix namedmat
. The first argument tomatrix()
is thedata
vector we just created. - 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. - We also specify the number of columns using the
ncol
argument, setting it to 2. This means our matrix will have 2 columns. - The
matrix()
function arranges the data in a column-major order by default, filling the matrix by columns. So, the resulting matrixmat
will look like this:1 5
2 6
3 7
4 8
- We then use the
rownames()
function to assign names to the rows of the matrix, setting them toc("Row1", "Row2", "Row3", "Row4")
. - We also use the
colnames()
function to assign names to the columns of the matrix, setting them toc("Col1", "Col2")
. - Finally, we use the
print()
function to display the matrixmat
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
- How to Create a Matrix in R ?
- How to Combine Matrices by Columns in R ?
- How to Combine Matrices by Rows in R ?
- How to Create Diagonal Matrices in R ?
- How to Access Matrix Elements using Indexing and Slicing in R ?
- How to get Matrix Size in R ?
- How to get Number of Rows in Matrix in R ?
- How to get Number of Columns in Matrix in R ?
- How to do Matrix Addition in R ?
- How to do Matrix Subtraction in R ?
- How to do Matrix Multiplication in R ?
- How to do Scalar Multiplication on a Matrix in R ?
- How to do Scalar Division on a Matrix in R ?
- How to do Element-Wise Operations in a Matrix in R ?
- How to Assign Row and Column Names in a Matrix in R ?
- How to get Row Names in a Matrix in R ?
- How to get Column Names in a Matrix in R ?
- How to find Transpose of a Matrix in R ?
- How to Extract the Diagonal of a Matrix in R ?
- How to find Determinant of a Matrix in R ?
- How to find Inverse of a Matrix in R ?
- How to find Rank of a Matrix in R ?
- How to Find Eigenvalues and Eigenvectors of a Matrix in R ?
- How to Perform Singular Value Decomposition (SVD) in R ?
- How to Perform QR Decomposition in R ?
- How to Perform Cholesky Decomposition in R ?
- How to Reshape Matrices in R ?
- How to Convert Data Frames to Matrices in R ?
- How to Create Identity Matrix in R ?
- How to Create Zero Matrix in R ?
- How to Create Ones Matrix in R ?
- How to Generate Random Matrices in R ?
- How to Calculate Row and Column Sums in R ?
- How to Calculate Row and Column Means in R ?
- How to Find Row and Column Max/Min in R ?
- How to Perform Element-Wise Comparisons in R ?