As a data scientist, you’ll be doing a lot of clustering. There are many types of clustering algorithms available, and you should be well-versed in using all of them. In this article, we’ll discuss a popular clustering algorithm, K-means, and see how it’s used in R.

You’ll find out the basic theory behind K-means clustering in R and how it’s used. We’ve also discussed a practical example later in the article. Be sure to bookmark this article for future reference. Read more about clustering analysis in Data Mining.

Before we begin discussing K means clustering in R, we should take a look at the types of clustering algorithms that are present so you can better understand how this algorithm deals with them.

Read: Top R Libraries in Data Science

**Types of Clustering**

When you group several objects in such a way that the objects which are the most similar to each other are in a close cluster, it’s called clustering. The distance between the objects could be relevant to their similarity. Similarity shows the strength of the relationship between two distinct objects in data science. Clustering is a popular data mining technique. Clustering finds its applications in numerous industries and areas, including image analysis, machine learning, data compression, pattern recognition, and many others.

Clustering is of two types – Hard and Soft. Let’s discuss each of them briefly.

- In a hard cluster, a data point would belong to a cluster totally, or it wouldn’t belong to it at all. There’s no in-between.
- In a soft cluster, a data object could be related to more than one cluster at once due to some likelihood or probability.

**Types of Clustering Algorithms**

Like there are different types of clusters, there are different types of clustering algorithms too. You can differentiate algorithms based on their cluster model. This means you can distinguish them based on how they form clusters. If we’d start talking about all kinds of clustering algorithms, this guide will become too long and far from the main point. So, we’ll only discuss a few prominent types of clustering algorithms. There is connectivity- based, centroid based, density-based, and distribution based clustering algorithms.

**Basic Concept of K-Means**

The basic concept of K-means is quite simple. K-means is related to defining the clusters so that the total within-cluster variation is as minimum as possible. There are a variety of k-means algorithms. The most common k-means algorithm is the Hartigan-Wong algorithm, which states that the total intra-cluster variation is equal to the sum of the squared distances Euclidean distances between centroids and their items:

W(Ck)=xiCk(xi–k)2

Here xi refers to a data point that belongs to the cluster Ck and k refers to the mean value of the data points present in the cluster Ck.

The value of xi should be such that the sum of the squared distance between xi and k is the minimum.

**What is the K-means Algorithm?**

To use the algorithm, we’ll first have to state the number of clusters, K, that will be present in our result. The algorithm first selects K objects randomly to act as initial cluster centers. We call those objects cluster centroids or means. Then we assign the remaining objects to their closest centroids. The Euclidean distance between the cluster centroids and the objects determines how close they are.

After we have assigned the objects to their respective centroids, the algorithm calculates the mean value of the clusters. After this re-computation, we recheck the observations to see if they might be closer to a different cluster. Then, we reassign the objects to centroids accordingly. We keep repeating these steps until assigning clusters stops. This means we stop repeating the iterations when the clusters formed in an iteration are the same as the ones in their previous iteration.

**Using K-Means Clustering (Example)**

Now that you know what is the K-means algorithm in R and how it works let’s discuss an example for better clarification. In this example, we’ll cluster the customers of an organization by using the database of wholesale customers. The data for this problem is available at the machine learning repository of Berkley UCI. You can check it out here.

First, we’ll read the data. And then get a summary of it. After reading the data and seeing its summary, you’ll see that there are some stark differences between the top consumers in different categories. You’ll find some outliers, which you can’t remove easily with normalization (or scaling). With this data, a business would want to see what their mid-range of customers buy most of the time. That’s because a company would have a decent idea of what their top customers buy.

To create a cluster of the mid-level customers, we should first get rid of the top layer of customers from each category. So we’ll remove the top 5 ones and create a new set. Here’s how we’ll do so:

top.n.custs <- function (data,cols,n=5) { #Requires some data frame and the top N to remove

idx.to.remove <-integer(0) #Initialize a vector to hold customers being removed

for (c in cols){ # For every column in the data we passed to this function

col.order <-order(data[,c],decreasing=T) #Sort column “c” in descending order (bigger on top)

#Order returns the sorted index (e.g. row 15, 3, 7, 1, …) rather than the actual values sorted.

idx <-head(col.order, n) #Take the first n of the sorted column C to

idx.to.remove <-union(idx.to.remove,idx) #Combine and de-duplicate the row ids that need to be removed

}

return(idx.to.remove) #Return the indexes of customers to be removed

}

top.custs <-top.n.custs(data,cols=3:8,n=5)

length(top.custs) #How Many Customers are needed to be Removed?

data[top.custs,] #Examine the available customers

data.rm.top<-data[-c(top.custs),] #Remove the required Customers

With this new file, we can start working on our cluster analysis. To perform the cluster analysis, we’ll use the following code:

set.seed(76964057) #Set the seed for reproducibility

k <-kmeans(data.rm.top[,-c(1,2)], centers=5) #Create 5 clusters, Remove columns 1 and 2

k$centers #Display cluster centers

table(k$cluster) #Give the count of data points in each cluster

When you have run this code on the given database, you’ll get these results:

- The first cluster would have high-quality detergents but the low quantity of fresh food products
- The third cluster would have more fresh product

You’ll need to use withinss and betweenss for a detailed interpretation of the results. k$withinss is equal to the sum of the distance’s square between each data object from the center of the cluster. The lower the range, the better would be the result. If the withinss measure is high in your data, it means there are many outliers present, and you need to perform data cleaning. k$betweenss is the sum of the distance’s square between different centers of the clusters. The distance between the cluster centers should be as high as possible.

Read: 6 More commonly used data structures in R

You should take help of trial and error to get the most accurate results. To do so, you’ll need to try out various values for K. When the graph of your results doesn’t show increment in the withinss of your clusters, that point would be the most suitable value for K. You can find the value of K through the following code:

rng<-2:20 #K from 2 to 20

tries <-100 #Run the K Means algorithm 100 times

avg.totw.ss <-integer(length(rng)) #Set up an empty vector to hold all of points

for(v in rng){ # For each value of the range variable

v.totw.ss <-integer(tries) #Set up an empty vector to hold the 100 tries

for(i in 1:tries){

k.temp <-kmeans(data.rm.top,centers=v) #Run kmeans

v.totw.ss[i] <-k.temp$tot.withinss#Store the total withinss

}

avg.totw.ss[v-1] <-mean(v.totw.ss) #Average the 100 total withinss

}

plot(rng,avg.totw.ss,type=”b”, main=”Total Within SS by Various K”,

ylab=”Average Total Within Sum of Squares”,

xlab=”Value of K”)

That’s it. Now you can use the graph you get from this code to get the best value for K and use it to get the required results. Use this example to try out your knowledge of K-means clustering in R. Here is all the code we’ve used in the example:

data <-read.csv(“Wholesale customers data.csv”,header=T)

summary(data)

top.n.custs <- function (data,cols,n=5) { #Requires some data frame and the top N to remove

idx.to.remove <-integer(0) #Initialize a vector to hold customers being removed

for (c in cols){ # For every column in the data we passed to this function

col.order <-order(data[,c],decreasing=T) #Sort column “c” in descending order (bigger on top)

#Order returns the sorted index (e.g. row 15, 3, 7, 1, …) rather than the actual values sorted.

idx <-head(col.order, n) #Take the first n of the sorted column C to

idx.to.remove <-union(idx.to.remove,idx) #Combine and de-duplicate the row ids that need to be removed

}

return(idx.to.remove) #Return the indexes of customers to be removed

}

top.custs <-top.n.custs(data,cols=3:8,n=5)

length(top.custs) #How Many Customers to be Removed?

data[top.custs,] #Examine the customers

data.rm.top <-data[-c(top.custs),] #Remove the Customers

set.seed(76964057) #Set the seed for reproducibility

k <-kmeans(data.rm.top[,-c(1,2)], centers=5) #Create 5 clusters, Remove columns 1 and 2

k$centers #Display cluster centers

table(k$cluster) #Give a count of data points in each cluster

rng<-2:20 #K from 2 to 20

tries<-100 #Run the K Means algorithm 100 times

avg.totw.ss<-integer(length(rng)) #Set up an empty vector to hold all of points

for(v in rng){ # For each value of the range variable

v.totw.ss<-integer(tries) #Set up an empty vector to hold the 100 tries

for(i in 1:tries){

k.temp<-kmeans(data.rm.top,centers=v) #Run kmeans

v.totw.ss[i]<-k.temp$tot.withinss#Store the total withinss

}

avg.totw.ss[v-1]<-mean(v.totw.ss) #Average the 100 total withinss

}

plot(rng,avg.totw.ss,type=”b”, main=”Total Within SS by Various K”,

ylab=”Average Total Within Sum of Squares”,

xlab=”Value of K”)

**Conclusion**

We hope you liked this guide. We’ve tried to keep it concise and comprehensive. If you have any questions about the K-means algorithm, feel free to ask us. We’d love to answer your queries.

If you are curious to learn about data science, check out our PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

#### Latest posts by Rohit Sharma (see all)

- How To Pick The Right Tableau Product For Your Requirement? - March 27, 2020
- Most Important Python Functions [With Examples] | Types of Functions - March 27, 2020
- Python Data Types [Completely Explained for Beginners] - March 27, 2020