Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Created September 17, 2013 17:38
Show Gist options
  • Save randyzwitch/6597784 to your computer and use it in GitHub Desktop.
Save randyzwitch/6597784 to your computer and use it in GitHub Desktop.
Repeated k-means to calculate elbow graph
#accumulator for cost results
cost_df <- data.frame()
#run kmeans for all clusters up to 100
for(i in 1:100){
#Run kmeans for each level of i, allowing up to 100 iterations for convergence
kmeans<- kmeans(x=dtm, centers=i, iter.max=100)
#Combine cluster number and cost together, write to df
cost_df<- rbind(cost_df, cbind(i, kmeans$tot.withinss))
}
names(cost_df) <- c("cluster", "cost")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment