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

后端 未结 16 1931
日久生厌
日久生厌 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条回答
  •  耶瑟儿~
    2020-11-21 06:25

    If you want control over how many NAs are valid for each row, try this function. For many survey data sets, too many blank question responses can ruin the results. So they are deleted after a certain threshold. This function will allow you to choose how many NAs the row can have before it's deleted:

    delete.na <- function(DF, n=0) {
      DF[rowSums(is.na(DF)) <= n,]
    }
    

    By default, it will eliminate all NAs:

    delete.na(final)
                 gene hsap mmul mmus rnor cfam
    2 ENSG00000199674    0    2    2    2    2
    6 ENSG00000221312    0    1    2    3    2
    

    Or specify the maximum number of NAs allowed:

    delete.na(final, 2)
                 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
    

提交回复
热议问题