Remove rows with all or some NAs (missing values) in data.frame

808    Asked by anurag_4280 in Data Science , Asked on Jun 4, 2021

Want to remove the lines from data frame from that :

  • Have NAs across all columns 

             a                               b    c       d     e        f
1 YASH00000206234    0   NA   NA   NA   NA
2 YASH00000199774    0    2        2       2      2
3 YASH00000221722    0   NA   NA   NA   NA
4 YASH00000207704    0   NA   NA   1       2
5 YASH00000207531    0   NA   NA   NA   NA
6 YASH00000221412    0   1        2      3       2 

I would like to get the data frame as follows :

             a                             b    c    d    e    f
2 YASH00000199774    0   2    2    2    2
6 YASH00000221412    0   1    2    3    2 
  • Have NAs in only some columns and the result I will get:

             a                              b   c    d     e    f    
2 YASH00000199774    0   2    2    2    2
4 YASH00000207704   0  NA  NA 1   2 
6 YASH00000221412    0   1    2    3    2

Answered by Ben PHILLIPS

(a)To r delete rows with na values, we use na.omit() function.

In your case:

final <- na.omit(dataframe)

Output:
             a b c d e f
2 YASH00000199774 0 2 2 2 2
6 YASH00000221412 0 1 2 3 2

(b) To remove r rows with NA by selecting specific columns from a data frame then you can use complete.cases() function.

In your case:

dataframe[complete.cases(dataframe[ , 5:6]),]

Output:
              a b c d e f
2 YASH00000199774 0 2 2 2 2
4 YASH00000207704 0 NA NA 1 2
6 YASH00000221412 0 1 2 3 2

Your Answer

Interviews

Parent Categories