相关疑难解决方法(0)

查找包含所有缺失值的列

我正在编写一个函数,需要检查(和哪个!)列(变量)是否具有所有缺失值(NA,<NA>).以下是该功能的片段:

test1 <- data.frame (matrix(c(1,2,3,NA,2,3,NA,NA,2), 3,3))
test2 <- data.frame (matrix(c(1,2,3,NA,NA,NA,NA,NA,2), 3,3))

na.test <-  function (data) {
  if (colSums(!is.na(data) == 0)){
      stop ("The some variable in the dataset has all missing value,
     remove the column to proceed")
      }
      }
na.test (test1)

Warning message:
In if (colSums(!is.na(data) == 0)) { :
  the condition has length > 1 and only the first element will be used
Run Code Online (Sandbox Code Playgroud)

Q1:为什么上述错误和任何修复?

Q2:有没有办法找到哪一列都有NA,例如输出列表(变量名或列号)?

r dataframe na

25
推荐指数
4
解决办法
5万
查看次数

标签 统计

dataframe ×1

na ×1

r ×1