I am new to R and I use a script I do not completely understand. It preprocesses a dataset for data mining. At one point, the data (stored in fil) should be centered and scaled and the NA values are replaced by values calculated with knnImpute.

preProcValues <- preProcess(fil, method = c("center", "scale","knnImpute"))
filpred <- predict(preProcValues, fil)

I get the following Error:

Error in nn2(old[, cols, drop = FALSE], new[, cols, drop = FALSE], k = k) :
NA/NaN/Inf in foreign function call (arg 1)

It seems to me that this error has to do with the way how knnImpute works. Is it possible that knnImpute uses surrounding columns to impute NA values? Because in the column where this stops to work there are a lot of NA values in one row of several columns.

Can anybody explain if this might be the cause of the error? Is anybody able to explain how knnImpute works or what else might be the cause?

  • $\begingroup$ You will have to give us a reproducible example (and the results of sessionInfo) to figure it out. $\endgroup$
    – topepo
    Commented Feb 1, 2014 at 1:17
  • $\begingroup$ Sorry, that will take a while, since i am not so fluent with R and I am not allowed to upload the data. I will try to create an example. $\endgroup$
    – aldorado
    Commented Feb 3, 2014 at 11:15
  • $\begingroup$ I had the exact same problem $\endgroup$
    – B_Miner
    Commented Jul 14, 2014 at 2:28

1 Answer 1


The knnImpute method uses the most similar (nearest) neighbors to impute the missing values using the average value of its neighbors for that column.

So, the columns used to determine the neighbors should be clean (not having NA/INF/NaN) and for the columns with the missing values you are trying to impute, they should have values to be used for taking average. I hope it helps.


Not the answer you're looking for? Browse other questions tagged or ask your own question.