remove rows with NA values in R dataframe

Remove Rows with NA Values in R

In this tutorial, we will look at how to remove rows in an R dataframe with NA values with the help of some examples.

How to drop rows with NA values in R?

remove rows with NA values in R dataframe

You can use the na.omit() function in R to remove rows with NA values from a dataframe. Pass the dataframe as an argument to the function.

The following is the syntax –

# remove rows with NA
na.omit(dataframe)

It returns a dataframe with rows with any NA values removed.

Steps to remove rows with NA in R dataframe

Let’s now look at a step-by-step example of using the above syntax –

Step 1 – Create a dataframe

First, let’s create a dataframe that we will be using throughout this tutorial.

# 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

We now have a dataframe containing information about some employees in an office. The dataframe has the 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.

Also, note that rows 3 and 4 contain one NA value each.

Step 2 – Remove rows with NA values using na.omit()

Let’s now remove rows that contain any missing values (NA in any column of the row).

For this, pass the dataframe as an argument to the na.omit() function.

# remove rows with NA
new_df = na.omit(employees_df)
# display the dataframe
print(new_df)

Output:

    Name Age Department Salary
1    Jim  26      Sales  80000
2 Dwight  28      Sales  81000
5  Kevin  30 Accounting  72000

The resulting dataframe has rows with missing values removed.

Summary – Remove rows with NA in R

In this tutorial, we looked at how to drop rows from a dataframe containing one or more NA value(s). The following is a short summary of the steps mentioned in this tutorial.

  1. Create a dataframe (skip this step if you already have a dataframe to operate on).
  2. Use the na.omit() function to remove the rows with any NA value. Pass the dataframe as an argument.

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.


Author

  • 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.

Scroll to Top