imputation
imputation copied to clipboard
Error: kNNImpute() fails if only 1 row contains missing
Hi,
in the special case that only 1 row contains missing values, kNNImpute() returns:
"Error in apply(x.missing, 1, function(i) { :
dim(X) must have a positive length")
As far as I can see, the sub-function impute.prelim() is responsible in line
prelim = impute.prelim(x)
.
If more than 2 rows contain missing, this function returns a matrix for impute.prelim()$x.missing
.
If only 1 row contains missings, it returns a named vector, which later on can not be handled by the apply-function
t(apply(x.missing, 1, function(i) {
My suggestion was to test, if inserting the drop=FALSE
option at the end of function impute.prelim() solves the problem:
if (byrow)
x.missing = cbind(1:nrow(x), x)[missing.rows.indices, ,drop=FALSE ]
else x.missing = rbind(1:ncol(x), x)[, missing.cols.indices ,drop=FALSE]
Thanks for reading :)