Find columns with all missing values

点点圈 提交于 2019-11-28 22:56:44

This is easy enough to with sapply and a small anonymous function:

sapply(test1, function(x)all(is.na(x)))
   X1    X2    X3 
FALSE FALSE FALSE 

sapply(test2, function(x)all(is.na(x)))
   X1    X2    X3 
FALSE  TRUE FALSE 

And inside a function:

na.test <-  function (x) {
  w <- sapply(x, function(x)all(is.na(x)))
  if (any(w)) {
    stop(paste("All NA in columns", paste(which(w), collapse=", ")))
  }
}

na.test(test1)

na.test(test2)
Error in na.test(test2) : All NA in columns 2

In dplyr

ColNums_NotAllMissing <- function(df){ # helper function
  as.vector(which(colSums(is.na(df)) != nrow(df)))
}

df %>%
select(ColNums_NotAllMissing(.))

example:
x <- data.frame(x = c(NA, NA, NA), y = c(1, 2, NA), z = c(5, 6, 7))

x %>%
select(ColNums_NotAllMissing(.))

or, the other way around

Cols_AllMissing <- function(df){ # helper function
  as.vector(which(colSums(is.na(df)) == nrow(df)))
}


x %>%
  select(-Cols_AllMissing(.))
Choukha Ram

To find the columns with all values missing

 allmisscols <- apply(dataset,2, function(x)all(is.na(x)));  
 colswithallmiss <-names(allmisscols[allmisscols>0]);    
 print("the columns with all values missing");    
 print(colswithallmiss);

To test whether columns have all missing values:

apply(test1,2,function(x) {all(is.na(x))})

To get which columns have all missing values:

  test1.nona <- test1[ , colSums(is.na(test1)) == 0]

The following command gives you a nice table with the columns that have NA values:

sapply(dataframe, function(x)all(any(is.na(x))))

It's an improvement for the first answer you got, which doesn't work properly from some cases.

dplyr approach to finding the number of NAs for each column:

df %>% 
  summarise_all((funs(sum(is.na(.))))) 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!