count number of rows in an R dataframe

Get the Number of Rows in R Dataframe

A dataframe in R is a data structure used to store data in a tabular form – in rows and columns. In this tutorial, we will look at how to get the number of rows in an R dataframe with the help of some examples.

How do I count the rows in a dataframe in R?

You can use the built-in nrow() function to count the number of rows in a dataframe in R. Pass the dataframe as an argument.

The following is the syntax –

# number of rows in dataframe
nrow(dataframe)

It returns the number of rows in the dataframe (including the rows with NA values).

Examples

Let’s now look at some examples of using the above syntax to count rows in a dataset in R.

Example 1 – Count rows in an R dataframe

First, let’s create a dataframe that we will be operating on.

# create a dataframe
employees_df = data.frame(
  "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin"),
  "Age"= c(26, 28, 29, 32, 30),
  "Department"= c("Sales", "Sales", "Accounting", "HR", "Accounting"),
  "Salary" = c(80000, 81000, 72000, 65000, 72000)
)
# display the dataframe
print(employees_df)

Output:

    Name Age Department Salary
1    Jim  26      Sales  80000
2 Dwight  28      Sales  81000
3 Angela  29 Accounting  72000
4   Tobi  32         HR  65000
5  Kevin  30 Accounting  72000

We now have a dataframe containing information about some employees working in an office. The dataframe has columns “Name”, “Age”, “Department”, and “Salary”.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

We can see that the above dataframe has five rows. Let’s now try to get this row count programmatically in R.

To get the row count of the above dataframe, we pass the dataframe as an argument to the nrow() function.

# number of rows in employees_df
print(nrow(employees_df))

Output:

[1] 5

We get the number of rows in the above dataframe as 5.

Example 2 – Count rows in an R dataframe with NA values

What happens if the dataframe you want to count the rows for has NA values?

Let’s find out.

First, we will create a dataframe with some NA values.

# create a dataframe
employees_df = data.frame(
  "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin"),
  "Age"= c(26, 28, NA, 32, 30),
  "Department"= c("Sales", "Sales", "Accounting", "HR", "Accounting"),
  "Salary" = c(80000, 81000, 72000, NA, 72000)
)
# display the dataframe
print(employees_df)

Output:

    Name Age Department Salary
1    Jim  26      Sales  80000
2 Dwight  28      Sales  81000
3 Angela  NA Accounting  72000
4   Tobi  32         HR     NA
5  Kevin  30 Accounting  72000

Here, we created a similar dataframe from the above example by replacing some values with NA. You can see that rows 3 and 4 have one NA value each.

Let’s now apply the nrow() function to this dataframe.

# number of rows in employees_df
print(nrow(employees_df))

Output:

[1] 5

We get the number of rows in the above dataframe as 5. Same as the result we got when the dataframe didn’t have any NA values. We can say that the nrow() function counts the rows in the dataframe irrespective of the NA values present.

If you do not want to count rows with any NA values, use the na.omit() function before applying the nrow() function.

# count rows without any NA values
print(nrow(na.omit(employees_df)))

Output:

[1] 3

Now we get the row count as 3. This is the number of rows that do not have any missing values in the above dataframe.

Summary – Number of Rows in R dataframe

In this tutorial, we looked at how to count the number of rows in a dataframe in R. Here’s a short summary of the steps mentioned in this tutorial –

  1. Use the nrow() function to get the number of rows of a dataframe in R. It counts the rows (including the ones with NA values).
  2. To omit the rows with any missing values, apply the na.omit() function before applying the nrow() function. This will give the number of rows without any missing values in the dataframe.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Gottumukkala Sravan Kumar
Scroll to Top