Crime Data Clustaring

KData <- read.csv("D:\\DataScience\\Assignments\\K Means Clustaring\\crime_data.csv")

colnames(KData)
## [1] "X"        "Murder"   "Assault"  "UrbanPop" "Rape"
attach(KData)



Data <- scale(KData[,2:5])

#Normalized Data
View(Data)
plot(Data)

km <- kmeans(Data,4)

str(km)
## List of 9
##  $ cluster     : int [1:50] 2 2 2 3 2 2 1 1 2 2 ...
##  $ centers     : num [1:4, 1:4] -0.629 1.005 -0.208 -1.173 -0.409 ...
##   ..- 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] 9.33 46.75 6.15 7.44
##  $ tot.withinss: num 69.7
##  $ betweenss   : num 126
##  $ size        : int [1:4] 10 20 10 10
##  $ iter        : int 2
##  $ ifault      : int 0
##  - attr(*, "class")= chr "kmeans"
km$cluster
##  [1] 2 2 2 3 2 2 1 1 2 2 1 4 2 3 4 3 3 2 4 2 1 2 4 2 2 3 3 2 4 1 2 2 2 4 1
## [36] 3 3 1 1 2 4 2 2 1 4 3 1 4 4 3
#install.packages("animation")

#km1 <- kmeans.ani(Data,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  5.050  136.70     79.3 17.590
## 2       2 12.165  255.25     68.4 29.165
## 3       3  6.880  136.50     60.6 19.330
## 4       4  2.680   70.10     51.0 10.910