Remove rows with all or some NAs (missing values) in data.frame
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
(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