I am trying to implement K means clustering in R, Here is what my data look like:

 Seq                RegionNames(Zip) X%year(PercentChange)
4002                   53147      -1.683282e-02
4003                   28504      -1.807185e-02
4004                   10591      -5.432917e-03
4005                   96761       1.151578e-02
4006                   32750       5.905045e-03
4007                   54904      -1.193602e-04
4008                   97140       2.667454e-02
4009                   33774       1.932240e-02
4010                   43616      -1.159712e-03
4011                   89011       3.021237e-02

I am trying to cluster zip codes (RegionNames) based on percentage change in 5 years(X5Year)

Here is my code

    newallHomesZip <- data.frame(allHomesZip$RegionName,allHomesZip$X5Year) #Making new data frame with only zip and 5 yearly percentage increase
    (cl <- kmeans(na.omit(newallHomesZip), 2))  #omiting Null values and trying to form two clusters
    plot(x, col = cl$cluster) #plotting

here is the plot I get :

enter image description here

Clearly the clusters are not good.

I don't really understand the Kmean method, I want to form clusters based on percent change as magnitude and zip code as identifier

  • 1
    $\begingroup$ Post some 10..20 lines of your input data. Link-only derrogations are not considered a good practice on StackOverflow, as the referred content may become un-retrievable in some future time. Thanks for your kinde re-consideration and post update. $\endgroup$
    – user3666197
    Oct 7, 2014 at 16:14
  • 2
    $\begingroup$ Also in your plot statement you're plotting 'x' which isn't defined in your problem at all. It wasn't even used when making clusters so if it's unrelated to the clusters you made then I'm not surprised the clusters aren't doing great $\endgroup$
    – Dason
    Oct 7, 2014 at 16:15
  • 2
    $\begingroup$ This question really isn't about programming; it's about understanding a particular statistical method. As such, it's probably a better fit for Cross Validated (that fact that you are using R is really irrelevant). $\endgroup$
    – MrFlick
    Oct 7, 2014 at 16:26
  • $\begingroup$ The "RegionName" variable got input as a factor because it was quoted. (I looked at the original data whereas user3666197 should have told you to post the output of dput so this could be seen, rather than posting screen output.) $\endgroup$
    – DWin
    Oct 7, 2014 at 16:26
  • $\begingroup$ Yes,it was poorly done by me, I tried to make some changes, also would note that my next post don't have the same mistakes, Thanks $\endgroup$
    – Snedden27
    Oct 7, 2014 at 16:31

1 Answer 1


K-means algorithm works with continuous dimensions, and the basic idea is that it considers two elements to be "similar" if their values are "near". This means that k-means only make sense when there is a sense of continuity in the dimensions. Similar zip codes are usually near, but those are hardly a continuous dimension, so it may not be the best clustering criteria; geographical coordinates would probably make more sense.

Also, another problem with k-means is scale: you should scale all the variables to a similar range, or otherwise one will have much more weight than the other.

Finally, it's not clear what are the axes of your plot. If you are using two given variables for clustering, you should use those in the plot. K-means clusters will only be related in the dimensions that you use in the algorithm; other dimensions may or may not follow the same clusters.

  • $\begingroup$ what the OP did is like clustering people by weight and including as the two variables their weight and name. $\endgroup$
    – Wayne
    Oct 8, 2014 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.