R language Access Menu

Creating clusters from data

In following example, first 4 columns of iris dataset are used to create 3 clusters using kmeans() function and plotted using clusplot() function of cluster package: 

Code:

> fit = kmeans(iris[1:4], 3)    
> library(cluster)
> clusplot(iris[1:4], fit$cluster)  

                       

The clusters produced can be compared with Species of each row available in 5th column of iris dataset: 

Code:

> table(iris[,5], fit$cluster)
              1  2  3
  setosa      0 33 17
  versicolor 46  0  4
  virginica  50  0  0

Another way to plot cluster is using plotcluster() function of fpc package: 

Code:

> fit = kmeans(iris[1:4], 3)
> library(fpc)
> plotcluster(iris[1:4], fit$cluster)

                      

 


    Comments & Feedback