# How to implement k-means cluster analysis algorithm correctly?

I am trying to implement the K-mean analysis with the Standard algorithm.

My implementation seems to work, but I noticed some strange behavior. If the k is close to half of the length of the list to be analyzed, I will get a set that is empty. I am not sure if it is the correct behavior.

I think the worst case is k equal to the length of the list, and each result sets has only 1 element. Empty result sets will happen if k is greater than the length of the list, but it is an invalid situation.

• I don't know if this is an appropriate question for this forum, but I have included the R code below. – suncoolsu Aug 2 '11 at 7:59

The behavior you describe is perfectly correct. Using such large sizes of $K$ w.r.t. to your list length is also one of the reasons why you get empty clusters. Be wise when choosing $K$ and your initial set of centroids (which I assume you sampled from your population).