![]() |
VOOZH | about |
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
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")
drop_na(name_of_the_column)
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
is.na() function first looks for na values in a column and then discards such rows.
is.na(name of the column)
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
This function functions similar to the above two methods
complete.cases(name of the column)
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