knnImputation for missing data I have a dataframe containing missing values. I want to impute them with knnImputation as shown below.

Now my question is: are data imputed row wise or column wise and is there a way to give this option in knnImputation?

A &lt;-c(1,2,3,4,5, NA, NA)
B &lt;-c(2,3,NA, 2,4, 7, 6)
C &lt;-c(2,3,4, NA, 5, 1,7)
D&lt;-21:27
data&lt;-data.frame(A,B, C, D)
new_data&lt;-knnImputation(data, k = 3, scale = T, meth = "weighAvg", distData = NULL)
The data are imputed using values from the same column

Here's a simplified example to show what's going on:

A &lt;-c(1,2,3,4,5)
B &lt;-c(2,3,NA, 2,4)
C &lt;- c(2,3,4, 6, 1)
data&lt;-data.frame(A,B,C)
data

#&gt;  A  B C
#&gt;1 1  2 2
#&gt;2 2  3 3
#&gt;3 3 NA 4
#&gt;4 4  2 6
#&gt;5 5  4 1

# I'm using scale = F to make things simpler
knnImputation(data, k = 3, scale = F, meth = "weighAvg", distData = NULL)

#&gt;  A        B C
#&gt;1 1 2.000000 2
#&gt;2 2 3.000000 3
#&gt;3 3 2.594272 4
#&gt;4 4 2.000000 6
#&gt;5 5 4.000000 1

# calculate euclidean distances, dropping column of interest
dist(data[,-2])

#&gt;1        2        3        4
#&gt;2 1.414214                           
#&gt;3 2.828427 1.414214                  
#&gt;4 5.000000 3.605551 2.236068         
#&gt;5 4.123106 3.605551 3.605551 5.099020

# use k rows with smallest distance to row of interest to calculate weighted average
# using exp(-dist) as weights
weight_sum &lt;- weight_sum = exp(-2.828427) + exp(-1.414214) + exp(-2.236068)
2*exp(-2.828427)/weight_sum + 3*exp(-1.414214)/weight_sum + 2*exp(-2.236068)/weight_sum

#&gt;2.594271


If you want to impute using values from the same row, all you have to do is transpose the matrix:

t(knnImputation(t(data), k = 2, scale = F, meth = "weighAvg", distData = NULL))

#&gt;     A        B C
#&gt;[1,] 1 2.000000 2
#&gt;[2,] 2 3.000000 3
#&gt;[3,] 3 3.086729 4
#&gt;[4,] 4 2.000000 6
#&gt;[5,] 5 4.000000 1


(I had to lower k because there are only 2 extra columns)