custonKmeans<-function(dataset=NA,k=NA){
if(is.na(dataset) || is.na(k)){
stop("You must input valid parameters!")
}
Eudist<-function(x,y){
distance<-sqrt(sum((x-y)^2))
return (distance)
}
rows.dataset<-nrow(dataset)
continue.change=TRUE
initPoint<-dataset[sample.int(rows.dataset,size = k),]
formerPoint<-initPoint
iterPoint<-matrix(0,nrow = k,ncol = ncol(dataset))
error.matrix<-matrix(0,nrow=rows.dataset,ncol=k)
while(continue.change){
cluster.matrix<-matrix(0,nrow=rows.dataset,ncol=k)
for(i in 1:rows.dataset){
for(j in 1:k){
error.matrix[i,j]<-Eudist(dataset[i,],formerPoint[j,])
}
}
for(i in 1:rows.dataset){
cluster.matrix[i,which.min(error.matrix[i,])]<-1
}
for(i in 1:k){
iterPoint[i,]<-apply(dataset[which(cluster.matrix[,i] == 1),],2,"mean")
}
all.true<-c()
for(i in 1:k){
if(all(formerPoint[i,] == iterPoint[i,]) == T){
all.true[i]<-TRUE
}
}
formerPoint = iterPoint
continue.change=ifelse(all(all.true) == T,F,T)
}
colnames(iterPoint)<-colnames(dataset)
out=list()
out[["centers"]]<-iterPoint
out[["distance"]]<-error.matrix
out[["cluster"]]<-rep(1,rows.dataset)
for(i in 1:rows.dataset){
out[["cluster"]][i]<-which(cluster.matrix[i,] == 1)
}
return(out)
}