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

后端 未结 16 1596
日久生厌
日久生厌 2020-11-21 05:49

I\'d like to remove the lines in this data frame that:

a) contain NAs across all columns. Below is my example data frame.



        
16条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 06:22

    Another option if you want greater control over how rows are deemed to be invalid is

    final <- final[!(is.na(final$rnor)) | !(is.na(rawdata$cfam)),]
    

    Using the above, this:

                 gene hsap mmul mmus rnor cfam
    1 ENSG00000208234    0   NA   NA   NA   2
    2 ENSG00000199674    0   2    2    2    2
    3 ENSG00000221622    0   NA   NA   2   NA
    4 ENSG00000207604    0   NA   NA   1    2
    5 ENSG00000207431    0   NA   NA   NA   NA
    6 ENSG00000221312    0   1    2    3    2
    

    Becomes:

                 gene hsap mmul mmus rnor cfam
    1 ENSG00000208234    0   NA   NA   NA   2
    2 ENSG00000199674    0   2    2    2    2
    3 ENSG00000221622    0   NA   NA   2   NA
    4 ENSG00000207604    0   NA   NA   1    2
    6 ENSG00000221312    0   1    2    3    2
    

    ...where only row 5 is removed since it is the only row containing NAs for both rnor AND cfam. The boolean logic can then be changed to fit specific requirements.

提交回复
热议问题