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

后端 未结 16 1672
日久生厌
日久生厌 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
    2020-11-21 06:07

    Also check complete.cases :

    > final[complete.cases(final), ]
                 gene hsap mmul mmus rnor cfam
    2 ENSG00000199674    0    2    2    2    2
    6 ENSG00000221312    0    1    2    3    2
    

    na.omit is nicer for just removing all NA's. complete.cases allows partial selection by including only certain columns of the dataframe:

    > final[complete.cases(final[ , 5:6]),]
                 gene hsap mmul mmus rnor cfam
    2 ENSG00000199674    0    2    2    2    2
    4 ENSG00000207604    0   NA   NA    1    2
    6 ENSG00000221312    0    1    2    3    2
    

    Your solution can't work. If you insist on using is.na, then you have to do something like:

    > final[rowSums(is.na(final[ , 5:6])) == 0, ]
                 gene hsap mmul mmus rnor cfam
    2 ENSG00000199674    0    2    2    2    2
    4 ENSG00000207604    0   NA   NA    1    2
    6 ENSG00000221312    0    1    2    3    2
    

    but using complete.cases is quite a lot more clear, and faster.

提交回复
热议问题