- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove rows containing missing value based on a particular column in an 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
- Related Articles
- How to remove rows from data frame in R based on grouping value of a particular column?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to extract a particular value based on index from an R data frame column?
- How to remove duplicate rows and sort based on a numerical column an R data frame?
- How to remove rows based on blanks in a column from a data frame in R?
- How to remove rows from an R data frame based on frequency of values in grouping column?
- How to remove rows that contains coded missing value for all columns in an R data frame?
- How to assign a column value in a data frame based on another column in another R data frame?
- How to select rows based on range of values of a column in an R data frame?
- How to get row index based on a value of an R data frame column?
- How to extract a data frame’s column value based on a column value of another data frame in R?
- How to select top rows of an R data frame based on groups of factor column?
- How to find the frequency of particular value in rows of an R data frame?
- How to extract columns based on particular column values of an R data frame that match\na pattern?
- How to delete rows of an R data frame based on string match?
