The Wayback Machine - https://web.archive.org/web/20240701124403/https://www.geeksforgeeks.org/how-to-find-number-of-rows-and-columns-in-a-matrix-in-r/
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


Similar Reads

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: # 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, 9), 3, 3, byrow = TRUE) # Calling rownames() Function rowname
1 min read
Binding rows and columns of a Data Frame in R - bind_rows() and bind_cols() Function
bind_rows() function in R Programming is used to combine rows of two data frames. Syntax: bind_rows(data1, data2, id) Parameter: id: dataframe identifier data1, data2: data frame to combine Example: Combining Rows # R program to illustrate # combine rows # Install dplyr package install.packages("dplyr") # Load dplyr package library("
2 min read
Read CSV file and select specific rows and columns in R
In this article, we are going to see how to read CSV file and select specific rows and columns in R Programming Language. CSV file: To import a CSV file into the R environment we need to use a pre-defined function called read.csv(). Pass filename.csv as a parameter within quotations. First, we need to set the path to where the CSV file is located u
1 min read
How to read Excel file and select specific rows and columns in R?
In this article, we will discuss how to read an Excel file and select specific rows and columns from it using R Programming Language. File Used: To read an Excel file into R we have to pass its path as an argument to read_excel() function readxl library. Syntax: read_excel(path) To select a specific column we can use indexing. Syntax: df [ row_inde
2 min read
Sum Across Multiple Rows and Columns Using dplyr Package in R
In this article, we are going to see how to sum multiple Rows and columns using Dplyr Package in R Programming language. The dplyr package is used to perform simulations in the data by performing manipulations and transformations. It can be installed into the working space using the following command : install.packages("dplyr")Calculating row sums
3 min read
Remove duplicate rows based on multiple columns using Dplyr in R
In this article, we will learn how to remove duplicate rows based on multiple columns using dplyr in R programming language. Dataframe in use: lang value usage 1 Java 21 21 2 C 21 21 3 Python 3 0 4 GO 5 99 5 RUST 180 44 6 Javascript 9 48 7 Cpp 12 53 8 Java 21 21 9 Julia 6 6 10 Typescript 0 8 11 Python 3 0 12 GO 6 6Removing duplicate rows based on t
4 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 necessary) rather than as numbers Example 1: # R p
2 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 whether the value should be returned as a factor of
2 min read
Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
cbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by columns. Syntax: cbind(x1, x2, ..., deparse.level = 1) Parameters: x1, x2: vector, matrix, data frames deparse.level: This value determines how the column names generated. The default value of deparse.level is 1. Example 1: # R program to illustrate # cbind
2 min read
Calculate the Sum of Matrix or Array columns in R Programming - colSums() Function
colSums() function in R Language is used to compute the sums of matrix or array columns. Syntax: colSums (x, na.rm = FALSE, dims = 1) Parameters: x: matrix or array dims: this is integer value whose dimensions are regarded as ‘columns’ to sum over. It is over dimensions 1:dims. Example 1: # R program to illustrate # colSums function # Initializing
2 min read
Article Tags :
three90RightbarBannerImg