summary still shows NAs after using both na.omit and complete.cases

后端 未结 2 2095
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-27 05:04

I am a grad student using R and have been reading the other Stack Overflow answers regarding removing rows that contain NA from dataframes. I have tried both na.omit and comple

2条回答
  •  独厮守ぢ
    2021-01-27 06:00

    is.na is not the proper function. You want complete.cases and you want complete.cases which is the equivalent of function(x) apply(is.na(x), 1, all) or na.omit to filter the data:

    That is, you want all rows where there are no NA values.

    < x <- data.frame(a=c(1,2,NA), b=c(3,NA,NA))
    > x
       a  b
    1  1  3
    2  2 NA
    3 NA NA
    
    > x[complete.cases(x),]
      a b
    1 1 3
    
    > na.omit(x)
      a b
    1 1 3
    

    Then this is assigned back to x to save the data.

    complete.cases returns a vector, one element per row of the input data frame. On the other hand, is.na returns a matrix. This is not appropriate for returning complete cases, but can return all non-NA values as a vector:

    > is.na(x)
             a     b
    [1,] FALSE FALSE
    [2,] FALSE  TRUE
    [3,]  TRUE  TRUE
    
    
    > x[!is.na(x)]
    [1] 1 2 3
    

提交回复
热议问题