title: knnImpute using categorical variables with caret package
body: <p>I have the following data.table, where each unique <code>x</code> value is associated with a unique <code>y</code> value. Then I force one <code>x</code> value as <code>NA</code> for purposes of the k-nearest neighbors exercise:</p>
  
  <pre><code>dt &lt;- data.table(x = rep(c(1:4), 3), 
                   y = rep(c("Brandon", "Erica", "Karyna", "Alex"), 3))
  dt[3, 1] &lt;- NA
  
  print(dt)
   #    x       y
   #1:  1 Brandon
   #2:  2   Erica
   #3: NA  Karyna
   #4:  4    Alex
   #5:  1 Brandon
   #6:  2   Erica
   #7:  3  Karyna
   #8:  4    Alex
   #9:  1 Brandon
  #10:  2   Erica
  #11:  3  Karyna
  #12:  4    Alex
  </code></pre>
  
  <p>Referencing the first answer to <a href="https://stackoverflow.com/questions/14023423/how-to-preprocess-features-when-some-of-them-are-factors">this question</a>, I created a binary matrix out of <code>dt$y</code> as so:</p>
  
  <pre><code>dt.a &lt;- model.matrix(~ y -1 , data = dt)
  dt2 &lt;- cbind(dt[, -2, with = FALSE], dt.a)
  
  print(dt2)
   #    x yAlex yBrandon yErica yKaryna
   #1:  1     0        1      0       0
   #2:  2     0        0      1       0
   #3: NA     0        0      0       1
   #4:  4     1        0      0       0
   #5:  1     0        1      0       0
   #6:  2     0        0      1       0
   #7:  3     0        0      0       1
   #8:  4     1        0      0       0
   #9:  1     0        1      0       0
  #10:  2     0        0      1       0
  #11:  3     0        0      0       1
  #12:  4     1        0      0       0
  </code></pre>
  
  <p>Using the <code>knnImpute</code> method from the <code>preProcess</code> function of the <code>caret</code> package, I would expect that the center-and-scaled output below of <code>dt3[1, 3]</code> would equal rows 7 and 12. But it does not. In fact, it looks to be almost equal the negative value of rows 7 and 12. </p>
  
  <pre><code>preobj &lt;- preProcess(dt2, method = "knnImpute")
  dt3 &lt;- predict(preobj, dt2)
  
  print(dt3)
   #             x      yAlex   yBrandon     yErica    yKaryna
   #1: -1.19857753 -0.5527708  1.6583124 -0.5527708 -0.5527708
   #2: -0.37455548 -0.5527708 -0.5527708  1.6583124 -0.5527708
   #3: -0.04494666 -0.5527708 -0.5527708 -0.5527708  1.6583124
   #4:  1.27348863  1.6583124 -0.5527708 -0.5527708 -0.5527708
   #5: -1.19857753 -0.5527708  1.6583124 -0.5527708 -0.5527708
   #6: -0.37455548 -0.5527708 -0.5527708  1.6583124 -0.5527708
   #7:  0.44946657 -0.5527708 -0.5527708 -0.5527708  1.6583124
   #8:  1.27348863  1.6583124 -0.5527708 -0.5527708 -0.5527708
   #9: -1.19857753 -0.5527708  1.6583124 -0.5527708 -0.5527708
  #10: -0.37455548 -0.5527708 -0.5527708  1.6583124 -0.5527708
  #11:  0.44946657 -0.5527708 -0.5527708 -0.5527708  1.6583124
  #12:  1.27348863  1.6583124 -0.5527708 -0.5527708 -0.5527708
  </code></pre>
  
  <p>Shouldn't <code>dt3$x</code>'s row 3 equal rows 7 and 11? If so, what do I need to change in my script? If not, why?</p>