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

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

    For your first question, I have a code that I am comfortable with to get rid of all NAs. Thanks for @Gregor to make it simpler.

    final[!(rowSums(is.na(final))),]
    

    For the second question, the code is just an alternation from the previous solution.

    final[as.logical((rowSums(is.na(final))-5)),]
    

    Notice the -5 is the number of columns in your data. This will eliminate rows with all NAs, since the rowSums adds up to 5 and they become zeroes after subtraction. This time, as.logical is necessary.

提交回复
热议问题