K means - List members of cluster

I am doing some k means clustering on a large data set. I believe I successfully did the clustering, however, I was wondering if there is a simple way to list all the members of each cluster?

The object returned by kmeans has a cluster element that labels each point with its cluster assignment. Here I use that to make a graph of the cluster assignments and a new data frame containing only the first cluster.

DF <- data.frame(Value = c(rnorm(25), rnorm(25, 3, 1), rnorm(25, -4, 1)))
K_Means <- kmeans(DF$Value, centers = 3)
DF$cluster <- K_Means$cluster
library(ggplot2)
library(dplyr)
ggplot(DF, aes(cluster, Value, color = factor(cluster))) + geom_point()

Grp1 <- DF %>% filter(cluster == 1) %>% select(Value)
Grp1
#>            Value
#> 1   0.8672576551
#> 2  -1.2995577493
#> 3   0.4383957088
#> 4   0.4620650247
#> 5  -0.0889177031
#> 6  -0.9683652668
#> 7  -0.2998884449
#> 8  -0.9940854756
#> 9  -0.1516251053
#> 10  0.4595380252
#> 11 -0.4510058591
#> 12  0.1188620410
#> 13 -1.3325386269
#> 14  0.5718944764
#> 15  0.6896010611
#> 16 -0.0007338013
#> 17  0.0268202571
#> 18  0.1088236997
#> 19 -0.8926220493
#> 20 -0.8382404442
#> 21  0.6970742347
#> 22 -1.3763336095
#> 23  0.6028022780
#> 24  0.6571363465
#> 25  1.4032860933
#> 26  0.4019730528
#> 27  0.6413751351
#> 28  1.0022838275
#> 29 -1.7860050194

Created on 2020-04-16 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.