To understand what a matrix is and how to create a matrix in R, go through this shot.
The length of a given matrix is the multiple of its number of rows and number of columns. To obtain the length of a matrix in R, we simply use the length()
function.
length(matrix)
The length()
function takes a single mandatory parameter value, i.e., the name of the matrix whose length we want to obtain.
The length()
function returns the length of the given matrix.
mymatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)length(mymatrix)
In line 1, we create a 2-by-2 matrix (a matrix with 2 rows and 2 columns) with the name mymatrix
.
In line 3, we obtain the length of mymatrix
by using the length()
function.
The
[1]
in the output represents that it is the first value which is returned by the function.