How to obtain the length of a matrix in R
Overview
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.
Syntax
length(matrix)
Parameter value
The length() function takes a single mandatory parameter value, i.e., the name of the matrix whose length we want to obtain.
Return value
The length() function returns the length of the given matrix.
Example
mymatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)length(mymatrix)
Explanation
-
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
mymatrixby using thelength()function.
The
[1]in the output represents that it is the first value which is returned by the function.