# K-Means Cluster Analysis for a 2D Random Point Set
mydata <- matrix(runif(40),ncol=2)
fit <- kmeans(mydata, 5) # 5 cluster solution
# get cluster means
aggregate(mydata,by=list(fit$cluster),FUN=mean)
## Group.1 V1 V2
## 1 1 0.2720483 0.1078595
## 2 2 0.1232850 0.7844528
## 3 3 0.4362333 0.5604447
## 4 4 0.7399861 0.8053710
## 5 5 0.7914742 0.4243538
# append cluster assignment
mycluster <- data.frame(mydata, fit$cluster)
library(animation)
## Warning: package 'animation' was built under R version 4.1.3
kmeans.ani(mycluster, centers=5, pch=1:5, col=1:5)