Reputation: 651
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?
Upvotes: 1
Views: 1502
Reputation: 25736
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
Upvotes: 3