Search code examples
rcluster-analysisk-means

R data output ordered by distance from cluster center


I am using R k-means procedure to cluster my data and what I want really is to look into rows which are closest to the cluster center.

Is there any easy way to do it without rewriting the function?


Solution

  • You can use the dist function to find the distances between the Kmeans centers and your data points.

    myData <- iris[, 1:4]
    myKmeans <- kmeans(myData, 3)
    DistancesToCenters <- as.matrix(dist(rbind(myKmeans$centers, myData)))[-(1:3),1:3]