title: Optimize for loop in R for kNN
body: <p>I have made this customized k-Nearest Neighbors algorithm (k=1) that can also be used on categorical variables (char, string). However, in terms of performance, it's slow because it uses <code>for</code> loop. Is there a way to optimize this loop so that it can perform better?</p>
  
  <pre><code>for (i in 1:nrow(test.data)) {
    a &lt;- c(test.data[i,1:ncol(training.data)-1])
  
    final &lt;- t((t(b) == a) * value)
    final[is.na(final)] &lt;- 0
    sum.value &lt;- rowSums(final)
  
    final1 &lt;- cbind(training.data, sum.value)
    final1 &lt;- final1[order(-sum.value),]
    final1 &lt;- final1[final1$sum.value &gt; 0,]
  
    suggestion &lt;- unique(final1[,ncol(training.data)])
  
    if (length(unique(training.data[sum.value == max.val, ncol(training.data)])) &lt; 5) {
      suggestion &lt;- suggestion[1:5]
      output.line &lt;- final1[!duplicated(final1$label),1:ncol(training.data)]
      output.line &lt;- output.line[1:5,]
    } else {
      suggestion &lt;- unique(training.data[sum.value == max.val, ncol(training.data)])
      output.line &lt;- unique(training.data[sum.value == max.val,])
    }
    output &lt;- rbind.fill(output,data.frame(t(c(test.ID = test.data[i,ncol(training.data),], suggestion))))
  
    sc &lt;- t(t(output.line[,1:ncol(training.data)-1]) == a)
    sc[is.na(sc)] &lt;- 0
    sc &lt;- rowSums(sc)
  
    output.line &lt;- cbind(output.line,sc)
    output.line &lt;- rbind.fill(cbind(test.data[i,],sc = 0),output.line)
    output2 &lt;- rbind(output2,output.line)
  }
  </code></pre>
  
  <p>The variables are as follows:</p>
  
  <pre><code>training.data = ~21 columns of data (with numbers and strings)
  test.data = ~20% of training.data
  label = obviously the label column
  b = training.data without the label column
  </code></pre>
  
  <p>This uses <code>plyr</code> package since it has <code>rbind.fill</code> function.</p>
  
  <p>Hope you can help on this matter. Thanks!</p>