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?
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”.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
- Create a dataframe (skip this step if you already have a dataframe to operate on).
- 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 –
- How to Create a DataFrame in R?
- How to Add a Row to a Dataframe in R?
- Select One or More Columns From R Dataframe
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.