# Optimal number of clusters in data retrieval

I need figure out a way to determine the best number of clusters for a group of data, I m aware you can run the kmeans function in a for loop and plot the data to see, but how do return the most optimal value computationally, like without looking at the plot?

```
for loop example:
wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var))
for (i in 2:15) wss[i] <- sum(kmeans(mydata,centers=i)$withinss)
```