I am performing a K means clustering using the kmeans
function in R
. After scaling my data. After I get the clusters, instead of getting individual cluster assignment, I want the the distance of each point from it's cluster center. Here is the code I am using.
data=read.csv("C:/Users/My_Folder/data.csv") # A data frame of 200 rows and 20 variables
traindata=data[,c(3,4)] # Features on which I want to do clustering
traindata=scale(traindata,center = T,scale=T) # Feature Scaling
km.result=rep(0,nrow(traindata))
km.cluster = kmeans(traindata, 2,iter.max=20,nstart=25)$cluster
cluster_1_num = sum(km.cluster==1)
cluster_2_num = sum(km.cluster==2)
if(cluster_1_num>cluster_2_num){
km.result[km.cluster==1]=1}
else{
km.result[km.cluster==2]=1}
data$cluster=km.result
This code effectively divides my 200 rows into 2 clusters. Instead of labels , is there a way to get distance of each point from it's cluster center. Do I need to re scale my data to original values.