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)
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”.
Also, note that rows 3 and 4 contain one NA value each.
Step 2 – Remove rows with NA values using
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
# remove rows with NA new_df = na.omit(employees_df) # display the dataframe print(new_df)
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.