title: How to compute distances between centroids and data matrix (for kmeans algorithm)
body: <p>I am a student of clustering and R. In order to obtain a better grip of both I would like to compute the distance between centroids and my xy-matrix for each iteration till it "converges". How can I solve for step 2 and 3 using R?</p>
  
  <pre><code>library(fields)
  x &lt;- c(3,6,8,1,2,2,6,6,7,7,8,8)
  y &lt;- c(5,2,3,5,4,6,1,8,3,6,1,7)
  
  df &lt;- data.frame(x,y) initial matrix
  a  &lt;- c(3,6,8)
  b  &lt;- c(5,2,3)
  
  df1 &lt;- data.frame(a,b) # initial centroids
  </code></pre>
  
  <p>Here is what I want to do:</p>
  
  <ol>
  <li><code>I0 &lt;- t(rdist(df, df1))</code>  after zero iteration</li>
  <li>Cluster objects based on minimum distance</li>
  <li>Determining the centroids based on the cluster average</li>
  <li>Repetition with I1</li>
  </ol>
  
  <p>I tried the <code>kmeans</code> function. But for some reasons it produces those centroids which have to come up at the end. That is I defined the start of:</p>
  
  <pre><code>start   &lt;- matrix(c(3,5,6,2,8,3), 3, byrow = TRUE)
  cluster &lt;- kmeans(df,centers = start, iter.max = 1) # one iteration
  </code></pre>
  
  <p><code>kmeans</code> doesn't allow me to track the movement of the centroids. Therefore I would like to do it "manually" by applying step 2 &amp; 3 using R.</p>