How to find number of rows and columns in a matrix in R
Last Updated :
25 Mar, 2024
In this article, we will see how to return the total number of rows and columns in a matrix in R Programming Language.
What is Matrix?
In R programming, Matrix is a two-dimensional, homogeneous data structure in which rows and columns run horizontally.
1. Getting the number of rows
nrow() function is used to return the number of rows of the specified object (Matrix/DataFrame etc). we will use this function which will return the total number of rows present in the given matrix.
Syntax
nrow(data)
Where data is the input matrix.
Let's create a numeric matrix with 4 rows and 4 columns and return total number of rows.
R
# Create matrix with 16 elements
data = matrix(1:16, nrow=4, ncol=4)
print(data)
# Total number of rows
rows=nrow(data)
cat("Number of rows: ", rows)
Output:
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
Number of rows: 4
Let's create a matrix with string type elements and return total number of rows.
R
# Create matrix with 6 elements
data = matrix(c("Python","Java","HTML","C","JSP","CSS"), nrow=3, ncol=2)
print(data)
# Total number of rows
rows=nrow(data)
cat("Number of rows: ", rows)
Output:
[,1] [,2]
[1,] "Python" "C"
[2,] "Java" "JSP"
[3,] "HTML" "CSS"
Number of rows: 3
2. Getting number of columns
ncol() function is used to return the number of columns of the specified object (Matrix/DataFrame etc). we will use this function which will return the total number of rows present in the given matrix.
Syntax:
ncol(data)
Where, data is the input matrix.
Let's create a numeric matrix with 4 rows and 5 columns and return total number of columns.
R
# Create matrix with 20 elements
data = matrix(1:20, nrow=4, ncol=5)
print(data)
# Total number of columns
columns=ncol(data)
cat("Number of columns: ", columns)
Output:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
Number of columns: 5
Let's create a matrix with string type elements and return total number of rows.
R
# Create matrix with 20 elements
data = matrix(c("Python","Java","HTML","C","JSP","CSS"), nrow=3, ncol=2)
print(data)
# Total number of columns
columns=ncol(data)
cat("Number of columns: ", columns)
Output:
[,1] [,2]
[1,] "Python" "C"
[2,] "Java" "JSP"
[3,] "HTML" "CSS"
Number of columns: 2
3. Getting number of rows and columns together
dim() function is used to get the dimension of the specified matrix. It will return total number rows and columns of the matrix.
Syntax:
dim(data)
Where, data is the input matrix.
Let's create a matrix with 4 rows and 5 columns and return total number of rows and columns at a time.
R
# Create matrix with 20 elements
data = matrix(1:20, nrow=4, ncol=5)
print(data)
# Total number of rows & columns
dimensions=dim(data)
cat("Number of rows & columns: ", dimensions)
Output:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
Number of rows & columns: 4 5
Similar Reads
Getting a Matrix of number of columns in R Programming - col() Function col() function in R Language is used to get a matrix which contains the number of columns of the matrix passed to it as argument. Syntax: col(x, as.factor=FALSE) Parameters: x: matrix as.factor: a logical value indicating whether the value should be returned as a factor of column labels (created if
2 min read
How to find the rank of a matrix in R A matrix is an arrangement of data in a row- and column-wise fashion or a matrix is nothing but a set of particular kinds of data like numbers. It has many applications in mathematics, Physics, engineering, etc. The number of rows and columns defines the matrix size called an order. For example, if
6 min read
How to find length of matrix in R In this article, we will examine various methods to find the length of a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data structure that is a collection of rows and columns. A matrix can able to contain data of various types such as numeric, characters, and
4 min read
Apply function to each column of matrix in R In this article, we will explore a method to apply the function to each matrix column by using R Programming Language. How to apply the function to each column of the matrix The function 'apply()' is used to apply the function to each column of the matrix. By using these methods provided by R, it is
3 min read
Getting a Matrix of number of rows in R Programming - row() Function In this article, we will discuss how to find the number of rows and columns in a matrix in R Programming Language. nrow() Function in Rnrow() function in R Language is used to get the row number of a matrix. Syntax: row(x, as.factor=FALSE) Parameters:x: matrixas.factor: a logical value indicating w
2 min read
Naming Rows and Columns of a Matrix in R Programming - rownames() and colnames() Function rownames() function in R Language is used to set the names to rows of a matrix. Syntax: rownames(x) <- value Parameters: x: Matrix value: Vector of names to be set Example: Python3 1== # R program to provide a name # to rows of a Matrix # Creating a 3X3 Matrix A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8,
1 min read