
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Rows with Missing Values in R Data Frame
If we want to remove rows containing missing values based on a particular column then we should select that column by ignoring the missing values. This can be done by using is.na function. For example, if we have a data frame df that contains column x, y, z and each of the columns have some missing values then rows of x without missing values can be selected as df[!is.na(df$x),].
Example
Consider the below data frame −
x1<−sample(c(NA,1,2,3,4),20,replace=TRUE) x2<−sample(c(NA,5,10),20,replace=TRUE) x3<−sample(c(NA,3,12,21,30),20,replace=TRUE) x4<−sample(c(NA,54,65),20,replace=TRUE) x5<−sample(c(NA,101,125,111),20,replace=TRUE) x6<−sample(c(NA,500),20,replace=TRUE) df<−data.frame(x1,x2,x3,x4,x5,x6) df
Output
x1 x2 x3 x4 x5 x6 1 4 10 21 54 NA NA 2 4 NA 21 65 NA 500 3 NA 5 NA NA 101 NA 4 3 5 NA NA NA NA 5 1 5 21 65 101 NA 6 NA 10 NA 65 111 500 7 2 NA NA NA NA NA 8 NA 5 NA NA 125 500 9 4 10 NA 54 NA NA 10 1 NA 12 NA 101 NA 11 4 NA 12 NA 101 NA 12 3 5 NA 65 111 NA 13 4 10 30 54 101 500 14 4 5 30 54 111 NA 15 3 5 NA 65 111 NA 16 1 NA 30 65 125 NA 17 1 5 3 65 125 500 18 3 5 NA NA 125 NA 19 NA NA 12 65 101 500 20 2 NA 21 54 111 NA
Selecting rows of x1 that does not contain missing values −
Example
df[!is.na(df$x1),]
Output
x1 x2 x3 x4 x5 x6 1 4 10 21 54 NA NA 2 4 NA 21 65 NA 500 4 3 5 NA NA NA NA 5 1 5 21 65 101 NA 7 2 NA NA NA NA NA 9 4 10 NA 54 NA NA 10 1 NA 12 NA 101 NA 11 4 NA 12 NA 101 NA 12 3 5 NA 65 111 NA 13 4 10 30 54 101 500 14 4 5 30 54 111 NA 15 3 5 NA 65 111 NA 16 1 NA 30 65 125 NA 17 1 5 3 65 125 500 18 3 5 NA NA 125 NA 20 2 NA 21 54 111 NA
Selecting rows of x2 that does not contain missing values −
Example
df[!is.na(df$x2),]
Output
x1 x2 x3 x4 x5 x6 1 4 10 21 54 NA NA 3 NA 5 NA NA 101 NA 4 3 5 NA NA NA NA 5 1 5 21 65 101 NA 6 NA 10 NA 65 111 500 8 NA 5 NA NA 125 500 9 4 10 NA 54 NA NA 12 3 5 NA 65 111 NA 13 4 10 30 54 101 500 14 4 5 30 54 111 NA 15 3 5 NA 65 111 NA 17 1 5 3 65 125 500 18 3 5 NA NA 125 NA
Selecting rows of x3 that does not contain missing values −
Example
df[!is.na(df$x3),]
Output
x1 x2 x3 x4 x5 x6 1 4 10 21 54 NA NA 2 4 NA 21 65 NA 500 5 1 5 21 65 101 NA 10 1 NA 12 NA 101 NA 11 4 NA 12 NA 101 NA 13 4 10 30 54 101 500 14 4 5 30 54 111 NA 16 1 NA 30 65 125 NA 17 1 5 3 65 125 500 19 NA NA 12 65 101 500 20 2 NA 21 54 111 NA
Selecting rows of x4 that does not contain missing values −
Example
df[!is.na(df$x4),]
Output
x1 x2 x3 x4 x5 x6 1 4 10 21 54 NA NA 2 4 NA 21 65 NA 500 5 1 5 21 65 101 NA 6 NA 10 NA 65 111 500 9 4 10 NA 54 NA NA 12 3 5 NA 65 111 NA 13 4 10 30 54 101 500 14 4 5 30 54 111 NA 15 3 5 NA 65 111 NA 16 1 NA 30 65 125 NA 17 1 5 3 65 125 500 19 NA NA 12 65 101 500 20 2 NA 21 54 111 NA
Selecting rows of x5 that does not contain missing values −
Example
df[!is.na(df$x5),]
Output
x1 x2 x3 x4 x5 x6 3 NA 5 NA NA 101 NA 5 1 5 21 65 101 NA 6 NA 10 NA 65 111 500 8 NA 5 NA NA 125 500 10 1 NA 12 NA 101 NA 11 4 NA 12 NA 101 NA 12 3 5 NA 65 111 NA 13 4 10 30 54 101 500 14 4 5 30 54 111 NA 15 3 5 NA 65 111 NA 16 1 NA 30 65 125 NA 17 1 5 3 65 125 500 18 3 5 NA NA 125 NA 19 NA NA 12 65 101 500 20 2 NA 21 54 111 NA
Selecting rows of x6 that does not contain missing values −
Example
df[!is.na(df$x6),]
Output
x1 x2 x3 x4 x5 x6 2 4 NA 21 65 NA 500 6 NA 10 NA 65 111 500 8 NA 5 NA NA 125 500 13 4 10 30 54 101 500 17 1 5 3 65 125 500 19 NA NA 12 65 101 500