Remove rows with NA in one column of R DataFrame (original) (raw)
Last Updated : 11 Apr, 2025
Columns of DataFrame in R Programming Language can have empty values represented by **NA. In this article, we are going to see how to remove rows with NA in one column. We will see various approaches to remove rows with NA values.
**Approach
- Create a data frame
- Select the column based on which rows are to be removed
- Traverse the column searching for na values
- Select rows
- Delete such rows using a specific method
**Remove rows with NA of one column in R DataFrame Using drop_na()
drop_na() Drops rows having values equal to NA. To use this approach we need to use "tidyr" library, which can be installed.
install.packages("tidyverse")
**Syntax:
drop_na(name_of_the_column)
R `
student=data.frame(name=c("Ram","Geeta","John","Paul", "Cassie","Jim","Dwight") ,maths=c(7,8,NA,9,10,8,9) ,science=c(5,7,6,8,NA,7,8) ,history=c(7,NA,7,7,NA,7,7))
print(student)
library(tidyr) student %>% drop_na(maths)
`
**Output:
name maths science history 1 Ram 7 5 7
2 Geeta 8 7 NA
3 John NA 6 7
4 Paul 9 8 7
5 Cassie 10 NA NA
6 Jim 8 7 7
7 Dwight 9 8 7
name maths science history
1 Ram 7 5 7
2 Geeta 8 7 NA
3 Paul 9 8 7
4 Cassie 10 NA NA
5 Jim 8 7 7
6 Dwight 9 8 7
**Remove rows with NA of one column in R DataFrame Using is.na()
is.na() function first looks for na values in a column and then discards such rows.
**Syntax:
is.na(name of the column)
R `
student=data.frame(name=c("Ram","Geeta","John","Paul", "Cassie","Jim","Dwight") ,maths=c(7,8,NA,9,10,8,9) ,science=c(5,7,6,8,NA,7,8) ,history=c(7,NA,7,7,NA,7,7))
print(student)
student[!is.na(student$science),]
`
**Output:
name maths science history 1 Ram 7 5 7
2 Geeta 8 7 NA
3 John NA 6 7
4 Paul 9 8 7
5 Cassie 10 NA NA
6 Jim 8 7 7
7 Dwight 9 8 7
name maths science history
1 Ram 7 5 7
2 Geeta 8 7 NA
3 John NA 6 7
4 Paul 9 8 7
6 Jim 8 7 7
7 Dwight 9 8 7
**Remove rows with NA of one column in R DataFrame Using complete.cases()
This function functions similar to the above two methods
**Syntax:
complete.cases(name of the column)
R `
student=data.frame(name=c("Ram","Geeta","John","Paul", "Cassie","Jim","Dwight") ,maths=c(7,8,NA,9,10,8,9) ,science=c(5,7,6,8,NA,7,8) ,history=c(7,NA,7,7,NA,7,7))
print(student)
student[complete.cases(student$history),]
`
**Output:
name maths science history 1 Ram 7 5 7
2 Geeta 8 7 NA
3 John NA 6 7
4 Paul 9 8 7
5 Cassie 10 NA NA
6 Jim 8 7 7
7 Dwight 9 8 7
name maths science history
1 Ram 7 5 7
3 John NA 6 7
4 Paul 9 8 7
6 Jim 8 7 7
7 Dwight 9 8 7