You are on page 1of 1

setwd("E:/01.PGDM/08.PGDM -2021/PGCM/01.

R for analytics/R scripts")


getwd()

install.packages("readxl")
library(readxl)

cust_sat=read_excel(file.choose())
attach(cust_sat)
fix(cust_sat)
names(cust_sat)
dim(cust_sat)
cust2=cust_sat[,2:12]
CU_S=scale(cust2)
d_cust=dist(CU_S, method="euclidean") #similarity matrix
d_cust
cust_clus=hclust(d_cust, method="ward.D2")
plot(cust_clus)
cust_cut=cutree(cust_clus, 3)
fr_cust=data.frame(cust2, cust_cut)
View(fr_cust)
#Customers belonging to the first cluster
cust_clus1=fr_cust[which(fr_cust$cust_cut==1),]
summary(cust_clus1[,1:11])
help("hclust")
dim(cust_clus1)
cust_clus2=fr_cust[which(fr_cust$cust_cut==2),]
summary(cust_clus2[,1:11])
cust_clus3=fr_cust[which(fr_cust$cust_cut==3),]
summary(cust_clus3[,1:11])

detach(cust_sat)

city=read_excel(file.choose())
library(readxl)
install.packages("cluster")
library(cluster)
install.packages("NbClust")
library(NbClust)
attach(city)
fix(city)
dim(city)
scities=scale(city[,2:7])
d_city=dist(scities, method="euclidean") #Similarity matrix
help("NbClust")
ncity=NbClust(scities, diss=d_city, distance=NULL,
method="kmeans", min.nc = 2,
max.nc = 15, index = "ch")
ncity

city_clust=kmeans(city[,2:7],2)

clusplot(city[,2:7], city_clust$cluster)

city_c=data.frame(city,city_clust$cluster)

View(city_c)
table(city_c$city_clust.cluster)

detach(city)

You might also like