【发布时间】:2016-01-27 10:00:30
【问题描述】:
在获取列数和名称后,我想过滤掉至少有一个缺失值的列。 我使用此函数获取仅包含缺失值的列的名称和数量,而不是将它们从数据框中过滤掉:
CheckColAllNulls <- if (ncol(Filter(function(x) all(is.na(x)), df)) > 0) {
cat("columns have only nulls:",ncol(Filter( function(x) all(is.na(x)), df)))
cat("columns names that have only nulls:",colnames(Filter( function(x) all(is.na(x)), df)))
df <- Filter(function(x) ! all(is.na(x)), df)
print("columns having only nulls removed ")
} else {
print("No columns having only nulls are found")
}
我尝试使用colSums 对至少有一个缺失值的列执行相同的操作,但没有成功。
CheckColNulls <- if ( colSums(is.na(df)) > 0) {
cat("columns have more than one null:",ncol(colSums(is.na(df)) > 0 ))
cat("columns names that have more than one null:",colnames(colSums(is.na(df)) > 0 ))
df <- Filter(function(x) colSums(is.na(x)) > 0), df)
print("columns having at least one null removed ")
} else {
print("No columns having at least one null are found")
}
这是我得到的错误:
Error in colSums(is.na(x)) :
'x' must be an array of at least two dimensions
In addition: Warning message:
In if (colSums(is.na(df)) > 0) { :
the condition has length > 1 and only the first element will be used
【问题讨论】:
-
请在您的语言中更准确。
NA与NULL不同。 -
警告可以被
if (c(3,7) > 0) print("hello")转载