Let's say I have a cluster vector generated by any clustering method, like the following on the iris data:
data(iris)
kmeans_res <- kmeans(x = iris[,c(1:4)], centers = 3)
kmeans_res$cluster
Is there an efficient way to create a matrix with zeros and ones based on this vector?
The rows and the columns of this matrix are the observations from the dataset from 1 to n. And the entries should be one, if e.g. observations 5 and 8 belong to the same cluster and zero otherwise.
The problem could be solved with a loop, but this doesn't seem very elegant. Can you think of another solution?