KData <- read.csv("C:\\Users\\Harisha\\Desktop\\Datascience Assignments\\K mean clustering\\crime_data.csv")
attach(KData)
attach(KData)
## The following objects are masked from KData (pos = 3):
##
## Assault, Murder, Rape, UrbanPop, X
Data <- scale(KData[,2:5])
View(Data)
plot(Data)

km <- kmeans(Data,4)
str(km)
## List of 9
## $ cluster : int [1:50] 1 2 2 1 2 2 4 4 2 1 ...
## $ centers : num [1:4, 1:4] 1.412 0.695 -0.962 -0.489 0.874 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : chr [1:4] "1" "2" "3" "4"
## .. ..$ : chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
## $ totss : num 196
## $ withinss : num [1:4] 8.32 19.92 11.95 16.21
## $ tot.withinss: num 56.4
## $ betweenss : num 140
## $ size : int [1:4] 8 13 13 16
## $ iter : int 2
## $ ifault : int 0
## - attr(*, "class")= chr "kmeans"
km$cluster
## [1] 1 2 2 1 2 2 4 4 2 1 4 3 2 4 3 4 3 1 3 2 4 2 3 1 2 3 3 2 3 4 2 2 1 3 4
## [36] 4 4 4 4 1 3 1 2 4 3 4 4 3 3 4
Final <- data.frame(km$cluster,KData)
View(Final)
x <- aggregate(KData[,2:5],by=list(km$cluster),FUN = mean)
x
## Group.1 Murder Assault UrbanPop Rape
## 1 1 13.93750 243.62500 53.75000 21.41250
## 2 2 10.81538 257.38462 76.00000 33.19231
## 3 3 3.60000 78.53846 52.07692 12.17692
## 4 4 5.65625 138.87500 73.87500 18.78125