Tutorial

Get the number of rows and columns in R

Published on August 4, 2022
author

Safa Mulani

Get the number of rows and columns in R

Hello, readers! In this article, we will be focusing on the concept of rows and columns in R i.e. get the number of rows and columns of an object in R programming, in detail.

So, let us begin!! :)

Be it a matrix or a data frame, we deal with the data in terms of rows and columns. In the data analysis field, especially for statistical analysis, it is necessary for us to know the details of the object i.e. the count of the rows and columns which represent the data values.

R programming provides us with some easy functions to get the related information at ease! So, let us have a look at it one by one.


The ncol() function in R programming

R programming helps us with ncol() function by which we can get the information on the count of the columns of the object.

That is, ncol() function returns the total number of columns present in the object.

Syntax:

ncol(object)

We need to pass the object that contains the data. Here, the object can be a data frame or even a matrix or a data set.

Example: 01

In the below example, we have created a matrix as shown below. Further, using ncol() function, we try to get the value of the number of columns present in the matrix.

rm(list = ls())
data = matrix(c(10,20,30,40),2,6)
print(data)
print('Number of columns of the matrix: ')
print(ncol(data))

Output:

> print(data)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   10   30   10   30   10   30
[2,]   20   40   20   40   20   40
> print('Number of columns of the matrix: ')
[1] "Number of columns of the matrix: "
> print(ncol(data))
[1] 6

Example 02:

Here, we have imported the Bank Loan Defaulter prediction dataset into the R environment using read.csv() function. You can find the dataset here!

Using ncol() function, we detect and extract the count of columns in the dataset.

rm(list = ls())
getwd()
#Load the dataset
dta = read.csv("bank-loan.csv",header=TRUE)
print('Number of columns: ')
print(ncol(dta))

Output:

Number of columns: 
9

The nrow() function in R programming

Having understood about columns, it’s now the time to discuss about the rows for an object.

R provides us nrow() function to get the rows for an object. That is, with nrow() function, we can easily detect and extract the number of rows present in an object that can be matrix, data frame or even a dataset.

Syntax:

nrow(object)

Example 01:

In this example, we have created a matrix using matrix() function in R. Further, we have performed the nrow() function to get the number of rows present in the matrix as shown–

rm(list = ls())
data = matrix(c(10,20,30,40),2,6)
print(data)
print('Number of rows of the matrix: ')
print(nrow(data))

Output:

> print(data)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   10   30   10   30   10   30
[2,]   20   40   20   40   20   40

"Number of rows of the matrix: "
[1] 2

Example 02:

Now, in this example, we have made use of the same Bank Load Defaulter dataset as mentioned the ncol() function section above!

Having loaded the dataset into the R environment, we make use of nrow() function to extract the number of rows present in the dataset.

rm(list = ls())
getwd()
#Load the dataset
dta = read.csv("bank-loan.csv",header=TRUE)
print('Number of rows: ')
print(nrow(dta))

Output:

"Number of rows: "
850

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, Stay tuned with us.

Till then, Happy Learning!! :)

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Safa Mulani

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.