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

后端 未结 16 2066
日久生厌
日久生厌 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:06

    I prefer following way to check whether rows contain any NAs:

    row.has.na <- apply(final, 1, function(x){any(is.na(x))})
    

    This returns logical vector with values denoting whether there is any NA in a row. You can use it to see how many rows you'll have to drop:

    sum(row.has.na)
    

    and eventually drop them

    final.filtered <- final[!row.has.na,]
    

    For filtering rows with certain part of NAs it becomes a little trickier (for example, you can feed 'final[,5:6]' to 'apply'). Generally, Joris Meys' solution seems to be more elegant.

提交回复
热议问题