Remove Rows with NA Values from an R Data Frame
Missing values in R are represented by NA. Depending on the analysis, you may need to remove rows containing any missing value, remove rows only when every selected value is missing, or check missingness in one specific column.
To remove rows of a data frame that contain one or more NA values, use complete.cases() as shown below.
resultDF = myDataframe[complete.cases(myDataframe),]
Here, myDataframe is the source data frame. The complete.cases(myDataframe) call returns TRUE for rows with no missing values and FALSE for rows containing at least one NA. The resulting data frame, resultDF, contains only complete rows.
Example: Remove Rows Containing Any NA Value
The following data frame contains one fully missing row and one partially missing row.
> DF1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21))
> DF1
x y
1 9 4
2 NA NA
3 7 NA
4 4 21
Row 2 contains NA in both columns. Row 3 contains an NA only in column y.
Use complete.cases() to keep only rows in which every column has a non-missing value.
> resultDF = DF1[complete.cases(DF1), ]
> resultDF
x y
1 9 4
4 4 21
Rows 2 and 3 are removed because each contains at least one NA.
Remove Rows Only When All Columns Are NA
Sometimes a partially complete row should be retained. In that case, remove a row only when the number of missing values in the row equals the total number of columns.
The following base R expression keeps rows whose count of NA values is less than the number of columns.
resultDF = mydataframe[rowSums(is.na(mydataframe[ , 0:ncol(mydataframe)])) < ncol(mydataframe), ]
is.na(mydataframe) creates a logical matrix that marks missing values. rowSums() counts the missing values in each row. A row is retained when that count is smaller than ncol(mydataframe).
Example: Keep Partially Complete Rows in an R Data Frame
Consider the same data frame again.
> DF1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21))
> DF1
x y
1 9 4
2 NA NA
3 7 NA
4 4 21
Only row 2 is missing in every column. Row 3 still contains the value 7, so it should remain.
> resultDF = DF1[rowSums(is.na(DF1[ , 0:ncol(DF1)])) < ncol(DF1), ]
> resultDF
x y
1 9 4
3 7 NA
4 4 21
>
The result removes the fully missing row while preserving the partially complete row.
Verify the All-NA Row Filter
The following repeated example verifies the same condition: rows are removed only when every column value is NA.
> DF1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21))
> DF1
x y
1 9 4
2 NA NA
3 7 NA
4 4 21
Row 2 satisfies the all-missing condition. Row 3 does not, because column x contains a value.
> resultDF = DF1[rowSums(is.na(DF1[ , 0:ncol(DF1)])) < ncol(DF1), ]
> resultDF
x y
1 9 4
3 7 NA
4 4 21
>
The output confirms that only the row containing NA in every column is deleted.
Remove Rows with NA in a Specific R Data Frame Column
To remove rows based on one column only, test that column with is.na(). Missing values in other columns do not affect the filter.
resultDF <- DF1[!is.na(DF1$y), ]
This keeps rows where column y is not NA. To require complete values in several selected columns, pass those columns to complete.cases().
resultDF <- DF1[complete.cases(DF1[c("x", "y")]), ]
Use na.omit() to Remove Incomplete Rows
na.omit() is a concise base R alternative when you want to remove every row containing at least one missing value.
resultDF <- na.omit(DF1)
The retained rows are the same as with DF1[complete.cases(DF1), ]. One difference is that na.omit() may attach an na.action attribute that records which rows were removed.
Remove NA Rows with dplyr and tidyr
In a tidyverse workflow, use tidyr::drop_na(). With no column arguments, it removes rows containing an NA in any column.
library(tidyr)
resultDF <- drop_na(DF1)
To remove rows only when a particular column is missing, name that column.
resultDF <- drop_na(DF1, y)
The equivalent dplyr filter is shown below.
library(dplyr)
resultDF <- DF1 |>
filter(!is.na(y))
Choose the Correct NA Row Removal Method
- Use
complete.cases(data)orna.omit(data)to remove rows containing anyNA. - Use
!is.na(data$column)to remove rows based on one specific column. - Use
rowSums(is.na(data)) < ncol(data)to remove only rows that are entirelyNA. - Use
tidyr::drop_na()when working in a tidyverse pipeline.
Before deleting rows, check how many observations will be lost. Removing incomplete cases can change sample size and may introduce bias when missing values are not random.
sum(!complete.cases(DF1))
colSums(is.na(DF1))
Frequently Asked Questions About Removing NA Rows in R
How do I delete every row containing an NA in R?
Use data[complete.cases(data), ] or na.omit(data). Both keep only rows with no missing values.
How do I remove rows with NA in one specific column?
Use data[!is.na(data$column_name), ]. With tidyverse code, use drop_na(data, column_name) or filter(data, !is.na(column_name)).
How do I remove rows only when all values are NA?
Use data[rowSums(is.na(data)) < ncol(data), ]. This retains rows that contain at least one non-missing value.
Does na.omit() remove rows or columns?
When applied to a data frame, na.omit() removes rows that contain missing values. It does not automatically remove columns.
Are NA and NaN handled the same way by these filters?
In R, is.na() returns TRUE for both NA and NaN. Therefore, complete.cases(), na.omit(), and filters based on is.na() also exclude rows containing NaN.
Editorial QA Checklist for R NA Row Examples
- Confirm whether the intended rule is any missing value, all missing values, or missingness in selected columns.
- Check that example output retains partially complete rows when using the all-NA filter.
- Verify that logical row conditions do not contain unresolved
NAvalues. - State how many rows are removed when data loss matters to the analysis.
- Use
drop_na()only after loading or namespacingtidyr.
Summary of Removing NA Rows from an R Data Frame
Use complete.cases() or na.omit() to remove rows containing any missing value. Use a column-specific is.na() condition when only one variable should control deletion, and use a row-wise missing-value count when only fully empty rows should be removed. For more R examples, see the R Tutorial.
TutorialKart.com