Skip to content

Data Science Tutorials

For Data Science Learners

  • How to Compare Two Excel Sheets for Differences
    How to Compare Two Excel Sheets for Differences Excel
  • Number to Percentage in R
    Number to Percentage in R R
  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • Best Data Visualization Books Course
  • Separate a data frame column into multiple columns
    Separate a data frame column into multiple columns-tidyr Part3 R
  • Multiple Plots to PDF in R R
  • Mastering R Programming for Data Science: Tips and Tricks R
  • Calculating Autocorrelation in R R

How to Find the Size of a Data Frame in R

Posted on August 10August 10 By Admin No Comments on How to Find the Size of a Data Frame in R

How to Find the Size of a Data Frame in R, we will explore how to find the size of a data frame in R.

A data frame is a fundamental data structure in R, and understanding its size is crucial for efficient data analysis.

We will discuss three functions that can be used to display the size of a data frame: nrow(), ncol(), and dim().

What are nrow(), ncol(), and dim()?

  • nrow() is used to display the number of rows in a data frame.
  • ncol() is used to display the number of columns in a data frame.
  • dim() is used to display the dimensions (rows and columns) of a data frame.

Example 1: Using nrow() to Display the Number of Rows

To use nrow() to display the number of rows in a data frame, simply type nrow(df) where df is the name of your data frame. For example, if we have a data frame called df with the following code:

df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F'),
points=c(909, 490, 886, 878, 895, 199),
assists=c(133, 258, 321, 239, 234, 225),
rebounds=c(102, NA, 224, 241, 218, 333))

We can use nrow() to display the number of rows as follows:

nrow(df)

This will output:

[1] 6

This means that our data frame has 6 rows.

Example 2: Using ncol() to Display the Number of Columns

To use ncol() to display the number of columns in a data frame, simply type ncol(df) where df is the name of your data frame. For example:

Locate the pattern in R ยป finnstats

ncol(df)

This will output:

[1] 4

This means that our data frame has 4 columns.

Example 3: Using dim() to Display Dimensions

To use dim() to display the dimensions (rows and columns) of a data frame, simply type dim(df) where df is the name of your data frame. For example:

dim(df)

This will output:

[1] 6 4

This means that our data frame has 6 rows and 4 columns.

Using Brackets with dim()

You can also use brackets with the dim() function to display only the rows or columns. For example:

dim(df)[1]

This will output:

[1] 6

This means that our data frame has 6 rows.

Similarly,

dim(df)[2]

This will output:

[1] 4

This means that our data frame has 4 columns.

Conclusion

In this article, we have learned how to find the size of a data frame in R using three functions: nrow(), ncol(), and dim().

We have also explored how to use brackets with dim() to display only the rows or columns.

By understanding how to find the size of a data frame, you can efficiently analyze your data and make informed decisions in your R projects.

  • Invisible Function in R: A Tutorial
  • Adding Space to Columns in R
  • Add text in specific location in R
  • Biases in Statistics Common Pitfalls
  • A 30-Day Plan for Data Science
  • Best Data Visualization Books
R

Post navigation

Previous Post: Filter a Vector in R
Next Post: Descriptive statistics in R

Related Posts

  • How to Implement the Sklearn Predict Approach
    How to Implement the Sklearn Predict Approach? R
  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • Descriptive Statistics in R R
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • How to deal with text in R
    How to deal with text in R R
  • How to Change X-Axis Labels of Barplot In R
    How to Change X-Axis Labels of Barplot In R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • droplevels in R with examples
    droplevels in R with examples R
  • Best Prompt Engineering Books R
  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R
  • Hypothesis Testing Examples
    Hypothesis Testing Examples-Quick Overview Statistics
  • Filter a Vector in R R
  • How to Check if a Directory Exists in R
    How to Check if a Directory Exists in R R
  • Top 10 online data science programmes
    Top 10 online data science programs Course
  • How to Avoid Overfitting
    How to Avoid Overfitting? Machine Learning

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme