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
How to Find Column Space of a Matrix
The concept of the column space of any specific matrix may well be considered one of the simplest ideas in linear algebra and is, without doubt, one of the crucial ideas in the study of the solutions to linear systems and in the manner that promotes understanding of the effects of linear transformat
10 min read
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 Fix in R: Arguments imply differing number of rows
In this article, we are going to see how to fix  Arguments that imply differing numbers of rows in R Programming Language. One error that we may encounter in R is: arguments imply differing number of rows: 6, 5 The compiler produces such an error when we try to create a data frame and the number of
2 min read
How to Create an Average Distance Matrix Based on Matching Prefix Column and Row Names in R?
Creating an average distance matrix based on matching prefix column and row names in R can be very useful in various fields, such as bioinformatics and text analysis. This process is based on calculating distances between elements and organizing these distances into a matrix format. By matching pref
3 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 Fix in R: incorrect number of dimensions
In this article, we will discuss how we can fix the "incorrect number of dimensions" error in R Programming Language. One common error that one may face in R is: Error in [x, 10] : incorrect number of dimensions The R compiler produces such an error when one tries to refer to an object by providing
2 min read
SQL Query to Find the Number of Columns in a Table
SQL stands for a structure query language, which is used in the database to retrieve data, update and modify data in relational databases like MySql, Oracle, etc. And a query is a question or request for data from the database, that is if we ask someone any question then the question is the query. S
4 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