
Cluster analysis is a foundational unsupervised learning methodology that facilitates the discovery of inherent structural patterns within multidimensional datasets through the systematic grouping of similar observations based on their intrinsic characteristics and spatial relationships. Through a variety of approaches such as centroid-based partitioning (k-means), hierarchical agglomeration, and density-based clustering (DBSCAN), practitioners can uncover meaningful data segments that can help identify natural groupings, outliers, and underlying patterns that may not be immediately apparent through traditional analytical methods.
The implementation of cluster analysis in R provides researchers and data scientists with a robust computational framework for exploring these latent structures, offering both statistical rigor and visual insight through a comprehensive set of clustering algorithms. Visualization of clustered results can further help shed light on our data.
In this article, we will learn how to use these methods in R.
Preparing the Data
First, we will load a dataset. Then, we will fix any missing values. Finally, we will scale the numbers.
# Load necessary libraries library(datasets) library(ggplot2) # Load a dataset data(iris) # Remove the species column iris_data <- iris[, -5] # Scale the data to normalize the values iris_data_scaled <- scale(iris_data) # View the first few rows of the dataset head(iris_data_scaled)

K-Means Clustering
K-means is one of the most popular clustering algorithms. It splits the data into K groups, where K is a number you choose. It works well when the groups are round and evenly spaced.
Here’s how it works:
- Choose the Number of Groups (k): You decide how many groups you want to create.
- Input Data: You give the kmeans() function the data you want to cluster.
- Run the Function: The function will find the best way to split your data into groups.
# Perform K-means clustering set.seed(123) kmeans_result <- kmeans(iris_data_scaled, centers = 3) # Add the cluster assignment to the original data iris$Cluster <- as.factor(kmeans_result$cluster) # Visualize the clusters ggplot(iris, aes(Petal.Length, Petal.Width, color = Cluster)) + geom_point() + labs(title = "K-Means Clustering")

In this example, we used the iris dataset and clustered it into three groups. The plot shows how the points are divided into clusters based on their similarities.
Hierarchical Clustering
Hierarchical clustering builds a tree of clusters. You do not need to decide the number of groups beforehand, making it flexible and useful for different types of data.
Here’s how it works:
- Calculate Distances: First, you find out how far apart the data points are. This step creates a distance matrix.
- Run the Function: You use hclust() to perform the clustering. This function takes the distance matrix and builds a tree of clusters.
- Visualize the Tree: You can create a plot to see the tree, called a dendrogram. This shows how the clusters are formed.
# Calculate the distance matrix dist_matrix <- dist(iris_data_scaled) # Perform hierarchical clustering hclust_result <- hclust(dist_matrix, method = "ward.D2") # Plot the dendrogram plot(hclust_result, main = "Hierarchical Clustering", xlab = "", sub = "", labels = FALSE) # Draw rectangles around the clusters rect.hclust(hclust_result, k = 3, border = 2:4)

In hierarchical clustering, the dendrogram helps you visualize how clusters are merged. You can cut the dendrogram at different levels to get the desired clusters.
DBSCAN Clustering
DBSCAN is a density-based clustering method that groups points based on the density of data points in the region. It’s beneficial for identifying clusters of arbitrary shapes and noise points.
DBSCAN needs two key parameters:
- Eps is the maximum distance between two points in a neighborhood.
- MinPts is the minimum number of points required to form a dense region.
Here’s how it works:
- Set Parameters: You need to choose Eps and MinPts.
- Input Data: You provide the dbscan() function with the data you want to cluster and the parameters you set.
- Run the Function: The function will look for dense areas in the data and group points that are close together. It can also identify points that don’t belong to any group as “noise.”
# Install the dbscan package
install.packages("dbscan")
library(dbscan)
# Perform DBSCAN clustering
dbscan_result <- dbscan(iris_data_scaled, eps = 0.5, minPts = 5)
# View the cluster assignments
print(dbscan_result$cluster)
# Add cluster assignments to the original data
iris$DBSCAN_Cluster <- as.factor(dbscan_result$cluster)
# Visualize the clusters
ggplot(iris, aes(Petal.Length, Petal.Width, color = DBSCAN_Cluster)) +
geom_point() +
labs(title = "DBSCAN Clustering")

DBSCAN detects the number of clusters based on the density of points. It also identifies “noise” points that do not belong to any cluster.
Conclusion
In conclusion, cluster analysis helps us group similar data. We looked at three methods in this tutorial: K-means, hierarchical, and DBSCAN. Each method finds patterns in different ways. Using R makes it easy to perform these analyses. Now you can explore your data and discover insights.
