1

I have a data frame with given structure.

District Value1  Value2  Value3

X         1200   1500   1420
Y         1456   1458   1247
Z         1245   1689   1200

I used K-means function in R to cluster Value1, Value2 and Value3 but that was not enough to find out which district falls in which cluster. I want to find out the cluster each district falls in, like:

District:  X     Y     Z
Cluster:  1     2     1

How do I do this in R?

coolscitist
  • 3,317
  • 8
  • 42
  • 59
Sudo
  • 651
  • 2
  • 7
  • 18

1 Answers1

3

You should try kmeans and have a look at ?kmeans (especially at the return value cluster):

df <- data.frame(District=c("X", "Y", "Z"), 
                 Value1=c(1200, 1500, 1420), 
                 Value2=c(1456, 1458, 1247),
                 Value3=c(1245, 1689, 1200))

#  df[,-1] excludes the first column (District)
km <- kmeans(df[,-1], centers=2)

km$cluster
#[1] 1 2 1
sgibb
  • 25,396
  • 3
  • 68
  • 74