Find names of columns which contain missing values

前端 未结 3 1062
忘掉有多难
忘掉有多难 2020-12-05 03:59

I want to find all the names of columns with NA or missing data and store these column names in a vector.

# create matrix
a <- c(1,2,3,4,5,N         


        
相关标签:
3条回答
  • 2020-12-05 04:54

    Like this?

    colnames(mymatrix)[colSums(is.na(mymatrix)) > 0]
    # [1] "aa" "ee"
    
    0 讨论(0)
  • 2020-12-05 05:03

    If you have a data frame with non-numeric columns, this solution is more general (building on previous answers):

    R 3.1 +

    names(which(sapply(mymatrix, anyNA)))

    or

    names(which(sapply(mymatrix, function(x) any(is.na(x)))))

    0 讨论(0)
  • 2020-12-05 05:05

    R 3.1 introduced an anyNA function, which is more convenient and faster:

    colnames(mymatrix)[ apply(mymatrix, 2, anyNA) ]
    

    Old answer:

    If it's a very long matrix, apply + any can short circuit and run a bit faster.

    apply(is.na(mymatrix), 2, any)
    #   aa    bb    cc    dd    ee 
    # TRUE FALSE FALSE FALSE  TRUE 
    colnames(mymatrix)[apply(is.na(mymatrix), 2, any)]
    # [1] "aa" "ee"
    
    0 讨论(0)
提交回复
热议问题