R nrow()

KyraThompson's avatar
Published Oct 7, 2022
Contribute to Docs

The nrow() function returns the number of rows in a data frame.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to code and clean and manipulate data for analysis and visualization with the R programming language.
    • Beginner Friendly.
      14 hours

Syntax

nrow(df_object)

The df_object parameter is the data frame passed into the nrow() function.

Example

The following example uses a CSV file named supplies.csv with the following information:

Item Quantity Needed Price
Brooms 15 8.5
Bucket 3 4
Cloth 6 2
Duster 10 5.5
Mop 5 10

The data within the file can be read in with the read.csv() function and the nrow() function can be used to retrieve the number of rows:

# Read file
df <- read.csv("supplies.csv")
# Retrieve number of rows
nrow(df)

This results in the following output:

5

All contributors

Contribute to Docs

Learn R on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to code and clean and manipulate data for analysis and visualization with the R programming language.
    • Beginner Friendly.
      14 hours