Open In App

How to find number of rows and columns in a matrix in R

Last Updated : 25 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads