library(plyr)
x<-runif(50)
x
## [1] 0.08590124 0.84694395 0.23069724 0.59780335 0.48366891 0.46025780
## [7] 0.15276470 0.89974665 0.77115886 0.16920652 0.47267674 0.21014326
## [13] 0.30862828 0.80182942 0.52545209 0.72106577 0.56691754 0.09493062
## [19] 0.57096017 0.32970759 0.77483763 0.37107605 0.06300773 0.28783639
## [25] 0.79848793 0.14486837 0.55780115 0.32725530 0.82444724 0.25111259
## [31] 0.11238841 0.59188419 0.92693583 0.66747551 0.25300160 0.78540836
## [37] 0.23779442 0.78328437 0.77957109 0.87397649 0.16463063 0.14601799
## [43] 0.99674210 0.12840154 0.04807942 0.23103104 0.98575453 0.12339122
## [49] 0.26941671 0.48283605
y<-runif(50)
y
## [1] 0.02638906 0.84520694 0.25325763 0.15261612 0.55341465 0.72977535
## [7] 0.83583496 0.29922258 0.60594644 0.81846688 0.86213177 0.80926390
## [13] 0.69271264 0.81164084 0.19949421 0.90164095 0.79969358 0.03436488
## [19] 0.94305712 0.75167167 0.61702358 0.91810628 0.57530495 0.97513016
## [25] 0.32150297 0.91966886 0.53147445 0.59518727 0.97734810 0.69729266
## [31] 0.84654169 0.89975004 0.49580110 0.25457336 0.11850549 0.93894514
## [37] 0.41888074 0.18185594 0.45879681 0.21264597 0.32050790 0.51510688
## [43] 0.54992702 0.19061913 0.95805676 0.30669514 0.10674933 0.50582735
## [49] 0.39126787 0.06544027
data<-cbind(x,y)
plot(data)
plot(data,type="n")
text(data,rownames(data))
km<-kmeans(data,4)
str(km)
## List of 9
## $ cluster : int [1:50] 2 4 2 3 4 4 1 3 4 1 ...
## $ centers : num [1:4, 1:2] 0.203 0.246 0.831 0.659 0.761 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : chr [1:4] "1" "2" "3" "4"
## .. ..$ : chr [1:2] "x" "y"
## $ totss : num 8.58
## $ withinss : num [1:4] 0.498 0.398 0.363 0.569
## $ tot.withinss: num 1.83
## $ betweenss : num 6.75
## $ size : int [1:4] 15 11 10 14
## $ iter : int 2
## $ ifault : int 0
## - attr(*, "class")= chr "kmeans"
km$cluster
## [1] 2 4 2 3 4 4 1 3 4 1 4 1 1 4 2 4 4 2 4 1 4 1 1 1 3 1 4 1 4 1 1 4 3 3 2
## [36] 4 2 3 3 3 2 1 3 2 1 2 3 1 2 2
library(animation)
km1<-kmeans.ani(data,4)