# k-means for set of integers [duplicate]

I'm not really sure where to begin with this but for a simplified example I have a set of integers:

{11, 22, 3, 12, 1, 23, 21, 13, 2}

I need to partition these into K=3 clusters with initial means of the clusters of 1,2,3 using Euclidean distances.

I'm assuming I need to find the distance of each value and compute new means but no idea how to go about it and everything I've read online doesn't seem to help

If someone can give me the basic ideas on how to tackle this much appreciated.

## marked as duplicate by gung♦, Andy, chlOct 19 '14 at 16:18

• – gung Oct 19 '14 at 15:31

In R, you could do something like this:

> kmeans(c(11, 22, 3, 12, 1, 23, 21, 13, 2), c(1, 2, 3))
K-means clustering with 3 clusters of sizes 1, 2, 6

Cluster means:
[,1]
1  1.0
2  2.5
3 17.0

Clustering vector:
[1] 3 3 2 3 1 3 3 3 2

Looking at ?kmeans the centers argument can be:

either the number of clusters, say k, or a set of initial (distinct) cluster centres

To get a vector that shows which cluster each element belongs to, use $cl, e.g. > kmeans(c(11, 22, 3, 12, 1, 23, 21, 13, 2), c(1, 2, 3))$cl
[1] 3 3 2 3 1 3 3 3 2