title: Function to calculate Euclidean distance in R
body: <p>I am trying to implement KNN classifier in R from scratch on iris data set and as a part of this i have written a function to calculate the Euclidean distance. Here is my code.</p>
  
  <pre><code>known_data &lt;- iris[1:15,c("Sepal.Length", "Petal.Length", "Class")]
  unknown_data &lt;- iris[16,c("Sepal.Length", "Petal.Length")]
  
  # euclidean distance
   euclidean_dist &lt;- function(k,unk) {
   distance &lt;- 0
   for(i in 1:nrow(k))
   distance[i] &lt;- sqrt((k[,1][i] - unk[,1][i])^2 + (k[,2][i] - unk[,2][i])^2)
   return(distance)
  } 
  
  euclidean_dist(known_data, unknown_data)
  </code></pre>
  
  <p>However, when i call the function it's returning the first value correctly and rest as NA. 
  Could anyone show where i could have gone wrong with the code? 
  Thanks in advance.</p>