How to obtain the dimension of a given matrix in R

Overview

To understand what a matrix is and how to create a matrix in R, click here.

Obtaining the dimension of a matrix

To obtain the dimension of a matrix in R, we use the dim() function.

Syntax

dim(matrix)

Parameter

The dim() function takes a single parameter, which is the matrix object.

Return value

The dim() function returns the dimension of a matrix passed to it.

Example

# creating a matrix
mymatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
# implementing the dim() function
dim(mymatrix)

Explanation

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.

Free Resources