Reputation: 922
I am extemely new to R and trying to deal with a kmeans object. Ideally what I would like to do is to take the list of cluster labels for each point in my data and replace the label with the corresponding center. Essentially, ending up with a matrix where each data point is represented by the value of the center of the cluster it has been placed into by kmeans.
Is there a way to do this efficiently instead of going through each entry manually and replacing the cluster label with the cluster center value?
Thanks!
Upvotes: 1
Views: 5171
Reputation: 42283
Is this what you're after? Extended from this answer:
# make some data
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
#
# do cluster analysis
(cl <- kmeans(x, 2))
#
# put cluster labels with data
out1 <- data.frame(cbind(x, clusterNum = cl$cluster))
#
# organise center coords to be ready for merging
centers <- data.frame(cbind(data.frame(cl$center[,1]),
data.frame(cl$center[,2]),
clusterNum=rownames(cl$center)))
#
# merge cluster center coords with data
out2 <- merge(out1, centers, all.x = TRUE)
#
# check output
out2
clusterNum x y cl.center...1. cl.center...2.
1 1 0.233161364 -0.04258146 0.01064895 0.01376516
2 1 -0.356284774 -0.59135602 0.01064895 0.01376516
3 1 -0.302272796 -0.24033113 0.01064895 0.01376516
4 1 -0.369299302 -0.24997660 0.01064895 0.01376516
5 1 -0.060454427 0.19711328 0.01064895 0.01376516
...
90 2 0.609833599 0.67729922 1.05184887 1.03445718
91 2 0.943306637 1.09420588 1.05184887 1.03445718
92 2 0.545053826 1.22620571 1.05184887 1.03445718
93 2 0.706921965 1.10326091 1.05184887 1.03445718
94 2 0.837644227 1.07121784 1.05184887 1.03445718
95 2 0.550863085 1.06977250 1.05184887 1.03445718
#
# Success! We have one dataframe that includes: raw data, cluster labels
# and cluster center coords
I used merge
to put the cluster center coords with the raw data, but no doubt there are more efficient ways (for example, that don't require cl$center
to be reorganised).
Upvotes: 4