Answers.Body: <p>The data are imputed using values from the same column</p>
  
  <p>Here's a simplified example to show what's going on:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>If you want to impute using values from the same row, all you have to do is transpose the matrix:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>(I had to lower k because there are only 2 extra columns)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-20T10:39:24.100Z
Answers.LastActivityDate: 2018-01-20T10:39:24.100Z
Answers.UserDisplayName: heathobrien
Body: <p>I have a dataframe containing missing values. I want to impute them with knnImputation as shown below.</p>
  
  <p>Now my question is: are data imputed row wise or column wise and is there a way to give this option in knnImputation?</p>
  
  <pre><code>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)
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-20T10:36:07.663Z
Comments.Text: According to the [code](https://github.com/cran/DMwR/blob/6fd4f0cd7a724f77586265f18e1a02cb48422bfc/R/utils.R) it seems like by row scaling by column but I am not sure. But you can change the orientation of your matrix by transposing it with `t` . Another option is to test it with a symmetric (or quasi-symmetric) matrix
Comments.UserDisplayName: llrs
Tags: <r>
Title: knnImputation for missing data
ThreadText: # knnImputation for missing data
  
  (Asked by: username_0 on 2018-01-19)
  
  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 <-c(1,2,3,4,5, NA, NA)
  B <-c(2,3,NA, 2,4, 7, 6)
  C <-c(2,3,4, NA, 5, 1,7)
  D<-21:27
  data<-data.frame(A,B, C, D)
  new_data<-knnImputation(data, k = 3, scale = T, meth = "weighAvg", distData = NULL)
  
  ```
  
  **Tags:** r
  
  > **Comments:**
  > **username_1**: According to the [code](https://github.com/cran/DMwR/blob/6fd4f0cd7a724f77586265f18e1a02cb48422bfc/R/utils.R) it seems like by row scaling by column but I am not sure. But you can change the orientation of your matrix by transposing it with `t` . Another option is to test it with a symmetric (or quasi-symmetric) matrix  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-01-20. Score: 4 | Accepted answer
  
  The data are imputed using values from the same column
  
  Here's a simplified example to show what's going on:
  
  ```
  A <-c(1,2,3,4,5)
  B <-c(2,3,NA, 2,4)
  C <- c(2,3,4, 6, 1)
  data<-data.frame(A,B,C)
  data
  
  #>  A  B C
  #>1 1  2 2
  #>2 2  3 3
  #>3 3 NA 4
  #>4 4  2 6
  #>5 5  4 1
  
  # I'm using scale = F to make things simpler
  knnImputation(data, k = 3, scale = F, meth = "weighAvg", distData = NULL)
  
  #>  A        B C
  #>1 1 2.000000 2
  #>2 2 3.000000 3
  #>3 3 2.594272 4
  #>4 4 2.000000 6
  #>5 5 4.000000 1
  
  # calculate euclidean distances, dropping column of interest
  dist(data[,-2])
  
  #>1        2        3        4
  #>2 1.414214                           
  #>3 2.828427 1.414214                  
  #>4 5.000000 3.605551 2.236068         
  #>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 <- 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
  
  #>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))
  
  #>     A        B C
  #>[1,] 1 2.000000 2
  #>[2,] 2 3.000000 3
  #>[3,] 3 3.086729 4
  #>[4,] 4 2.000000 6
  #>[5,] 5 4.000000 1
  
  ```
  
  (I had to lower k because there are only 2 extra columns)
  
  
  ---
  
  