How to find the x-value of each centroid in a kmeans

I am using the kmeans function to plot some data into 2 clusters. This works fine. However, I now have a more difficult problem. I know that by default the clusters are named 1 and 2, and that which cluster gets assigned to which number is random given that the cluster's centroid creation is started through randomly generated points. The cluster on the right could be labeled as 1 or it could end up being the one labeled as 2. I need to determine this.

It seems that the easiest way to do this would be to find the x-value for each centroid, and then compare them to find which one is to the right of the other. This is the silly thing I tried that did not work (where "" contains the result of the kmeans function, and "idealpoint" is the x value in the plot):

if($centers[1]$idealpoint >$centers[2]$idealpoint)

I think it's mainly just that I'm not clear on the correct syntax to target the value I'm trying to get. I hope that the above code that did not work nonetheless makes it clear what I'm trying to do, and that someone can show me the correct way to do what I'm trying to do.

if you want to know which of some number of clusters has the maximum center for a variable called 'idealpoint' then the following should work to tell you that.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.