Search code examples
rgraphadjacency-matrix

How to get adjacency matrix of each detected cluster in R?


Is there a simple way to convert each cluster detected by an k-means to an adjacency matrix in R?


Solution

  • Sure, you could bind the clusters to the data.

    res <- kmeans(as.matrix(iris[1:2]), 2)
    cbind(res$cluster, iris[1:2])
    

    To split the matrix into separate clusters

    split(cbind(res$cluster, iris[1:2]), res$cluster)