Show columns with NAs in a data.frame

后端 未结 3 1797
甜味超标
甜味超标 2021-02-01 05:57

I\'d like to show the names of columns in a large dataframe that contain missing values. Basically, I want the equivalent of complete.cases(df) but for columns, not rows. Some o

3条回答
  •  臣服心动
    2021-02-01 06:40

    One way...

    nacols <- function(x){
      y <- sapply(x, function(xx)any(is.na(xx)))
      names(y[y])
    }  
    
    nacols(tmp)
    [1] "y" "z"
    

    Explanation: since the result y is a logical vector, names(y[y]) returns the names of y for only those cases where y is TRUE.

提交回复
热议问题