我正在编写一个函数,它需要检查是否(以及哪个!)列(变量)具有所有缺失值(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
Q1:为什么会出现上述错误和任何修复?
Q2:有什么方法可以找到哪些列有 all NA
,例如输出列表(变量名或列号)?