-2

I am running a kmeans clustering to identify labeled data. I ran pca and then kmeans and got the following plot using ggbiplot:

enter image description here

Now, how can I determine which point belongs to which group in table format. That is, in my original data, I would like to label each point with its group.

LulY
  • 976
  • 1
  • 9
  • 24
wisamb
  • 470
  • 3
  • 11

1 Answers1

1

Assuming that the name of your dataframe is df and you want k clusters. After you run the k means function...

# K-Means CA
fit <- kmeans(df, k) # where k is the number of clusters

... you must include the groups that where produced from the fit into your dataframe

# add clusters to the dataframe
df$clusters <- fit$cluster
df
             a          b clusters
1  -0.96193342 -0.7447816        1
2  -0.29252572 -1.1312186        1
3   0.25878822 -0.7163585        1
4  -1.15213189  0.2526524        1
5   0.19578283  0.1520457        1
6   0.03012394 -0.3076564        1
7   0.08541773 -0.9530173        1
8   1.11661021 -0.6482428        2
9  -1.21885742  1.2243136        1
10  1.26736872  0.1998116        2

Data used in the example

set.seed(3)
n <- 10
k <- 2
df <- data.frame(a= rnorm(n), b= rnorm(n))

You can also have a look here.

LulY
  • 976
  • 1
  • 9
  • 24