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

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

    I am a synthesizer:). Here I combined the answers into one function:

    #' keep rows that have a certain number (range) of NAs anywhere/somewhere and delete others
    #' @param df a data frame
    #' @param col restrict to the columns where you would like to search for NA; eg, 3, c(3), 2:5, "place", c("place","age")
    #' \cr default is NULL, search for all columns
    #' @param n integer or vector, 0, c(3,5), number/range of NAs allowed.
    #' \cr If a number, the exact number of NAs kept
    #' \cr Range includes both ends 3<=n<=5
    #' \cr Range could be -Inf, Inf
    #' @return returns a new df with rows that have NA(s) removed
    #' @export
    ez.na.keep = function(df, col=NULL, n=0){
        if (!is.null(col)) {
            # R converts a single row/col to a vector if the parameter col has only one col
            # see https://radfordneal.wordpress.com/2008/08/20/design-flaws-in-r-2-%E2%80%94-dropped-dimensions/#comments
            df.temp = df[,col,drop=FALSE]
        } else {
            df.temp = df
        }
    
        if (length(n)==1){
            if (n==0) {
                # simply call complete.cases which might be faster
                result = df[complete.cases(df.temp),]
            } else {
                # credit: http://stackoverflow.com/a/30461945/2292993
                log <- apply(df.temp, 2, is.na)
                logindex <- apply(log, 1, function(x) sum(x) == n)
                result = df[logindex, ]
            }
        }
    
        if (length(n)==2){
            min = n[1]; max = n[2]
            log <- apply(df.temp, 2, is.na)
            logindex <- apply(log, 1, function(x) {sum(x) >= min && sum(x) <= max})
            result = df[logindex, ]
        }
    
        return(result)
    }
    

提交回复
热议问题