To understand what a matrix is and how to create a matrix in R, click here.
To obtain the dimension of a matrix in R, we use the dim()
function.
dim(matrix)
The dim()
function takes a single parameter, which is the matrix object.
The dim()
function returns the dimension of a matrix passed to it.
# creating a matrixmymatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)# implementing the dim() functiondim(mymatrix)
We create 2x2 matrix (a matrix with 2 rows and 2 columns) mymatrix
. Using the dim()
function, we check for the dimensions of the matrix. As we can see from the output, it is a 2x2 matrix.