Omit rows containing specific column of NA

前端 未结 8 1127
感动是毒
感动是毒 2020-11-27 10:39

I want to know how to omit NA values in a data frame, but only in some columns I am interested in.

For example,

DF <- data.frame(x =          


        
相关标签:
8条回答
  • 2020-11-27 10:58

    Just try this:

    DF %>% t %>% na.omit %>% t
    

    It transposes the data frame and omits null rows which were 'columns' before transposition and then you transpose it back.

    0 讨论(0)
  • 2020-11-27 11:00

    You could use the complete.cases function and put it into a function thusly:

    DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22))
    
    completeFun <- function(data, desiredCols) {
      completeVec <- complete.cases(data[, desiredCols])
      return(data[completeVec, ])
    }
    
    completeFun(DF, "y")
    #   x  y  z
    # 1 1  0 NA
    # 2 2 10 33
    
    completeFun(DF, c("y", "z"))
    #   x  y  z
    # 2 2 10 33
    

    EDIT: Only return rows with no NAs

    If you want to eliminate all rows with at least one NA in any column, just use the complete.cases function straight up:

    DF[complete.cases(DF), ]
    #   x  y  z
    # 2 2 10 33
    

    Or if completeFun is already ingrained in your workflow ;)

    completeFun(DF, names(DF))
    
    0 讨论(0)
  • 2020-11-27 11:02

    Hadley's tidyr just got this amazing function drop_na

    library(tidyr)
    DF %>% drop_na(y)
      x  y  z
    1 1  0 NA
    2 2 10 33
    
    0 讨论(0)
  • 2020-11-27 11:02

    Omit row if either of two specific columns contain <NA>.

    DF[!is.na(DF$x)&!is.na(DF$z),]
    
    0 讨论(0)
  • 2020-11-27 11:08

    It is possible to use na.omit for data.table:

    na.omit(data, cols = c("x", "z"))
    
    0 讨论(0)
  • 2020-11-27 11:13

    Try this:

    cc=is.na(DF$y)
    m=which(cc==c("TRUE"))
    DF=DF[-m,]
    
    0 讨论(0)
提交回复
热议问题