Doing Customer Segmentation with R
Customer segmentation involves dividing a customer base into groups with similar traits. This article will show you how to segment customers using R.

Image by Editor | Midjourney
Customer segmentation groups customers by their traits. This helps businesses know what different customers want and need. Using R, companies can easily segment their customers. This article will explain how to do customer segmentation with R.
Introduction to Customer Segmentation
Customer segmentation means splitting customers into different groups. These groups are based on traits like age, buying habits, or preferences. The aim is to understand customers better and make marketing more relevant.
Segmentation helps businesses create personalized marketing, improve products, and make customers happier. It allows companies to use their resources better and target their messages to the right people. This way, businesses build better customer relationships and grow.
Customer Segmentation Techniques
Customer segmentation techniques help businesses categorize their customer base into distinct groups based on various characteristics. They work best with different types of data and business needs. Here are some of the most common techniques used for customer segmentation:
- K-Means Clustering: Groups data into K clusters by minimizing the differences within each cluster. It works well for round-shaped clusters but needs you to decide the number of clusters first.
- Hierarchical Clustering: Creates a tree-like structure of clusters by repeatedly merging or splitting. It gives a detailed view but can be slow with large datasets.
- PCA (Principal Component Analysis): Reduces the number of features. It transforms the data into key components that hold the most variation.
Let's see how to perform customer segmentation using K-means clustering in R.
Import and Clean Your Data
To begin customer segmentation, you first need to import your dataset into R. Once the data is imported, it often requires cleaning. Cleaning involves removing missing values and duplicates to ensure the dataset is accurate and reliable.
# Load necessary libraries
library(dplyr)
library(tidyr)
library(ggplot2)
# Import your data
data <- read.csv("customer.csv")
# Clean the data
data <- data %>%
drop_na() %>%
distinct()
# Print the first 5 rows
head(data, 5)
The dataset includes customers' age, gender, annual income, and spending scores.

Prepare the Data for Clustering
Preparing the data is a crucial step before performing clustering. The 'Annual Income' and 'Spending Score' columns are selected and scaled.
# Select columns for clustering
clustering_data <- data %>%
select(Annual_Income, Spending_Score)
# Scale the data
scaled_data <- scale(clustering_data)
Determine the Optimal Number of Clusters
The Elbow Method is a way to find the best number of clusters in K-Means. It checks how the total within-cluster sum of squares (WCSS) changes as you add more clusters. The goal is to find where the WCSS slows down. This forms an "elbow" shape in the graph. This point shows the best number of clusters to use.
Here's how to implement the Elbow Method in R:
# Calculate total within-cluster sum of squares for different numbers of clusters
wcss <- vector()
for (i in 1:10) {
kmeans_result <- kmeans(scaled_data, centers = i)
wcss[i] <- sum(kmeans_result$tot.withinss)
}
# Plot the WCSS to visualize the Elbow Method
plot(1:10, wcss, type = "b", pch = 19, frame = FALSE,
xlab = "Number of Clusters", ylab = "Total Within-Cluster Sum of Squares (WCSS)",
main = "Elbow Method for Finding Optimal Number of Clusters")

This plot displays the Total Within-Cluster Sum of Squares (WCSS) for different numbers of clusters, ranging from 1 to 10. Based on this plot, 6 clusters are recommended for performing K-Means Clustering.
Perform K-Means Clustering
K-Means clustering groups your data into clusters. Decide how many clusters you want before starting. Use the 'kmeans' function in R to perform clustering.
# Perform K-Means clustering
kmeans_result <- kmeans(scaled_data, centers = 6)
Add Cluster Assignments to Original Data
After clustering, it's important to integrate the results into the original dataset. This allows you to analyze the clusters within the context of your original data.
# Add cluster assignments to original data
data$Cluster <- kmeans_result$cluster
head(data)

This code assigns cluster labels from the K-means Clustering result to the 'Cluster' column in the data dataframe.
Visualize Clusters
Visualizing clusters helps you see how data points are grouped. You can use graphs to show different clusters. Use colors to represent different clusters. This makes it easy to see patterns and differences.
# Visualize clusters
ggplot(data, aes(x = Annual_Income, y = Spending_Score, color = factor(Cluster))) +
geom_point() +
labs(title = "Customer Segments", x = "Income", y = "Spending Score", color = "Cluster")

The graph shows customer segments based on Annual Income and Spending Score. Each color represents a different cluster. It helps to see how customers are grouped and can guide targeted marketing strategies.
Conclusion
Customer segmentation helps businesses tailor their strategies for different groups of customers. In R, you begin by loading and cleaning your data. Next, you prepare the data and use clustering techniques to group similar customers. Finally, you visualize these groups to understand them better. This understanding can help you improve marketing and increase sales. For even deeper insights, you can use advanced methods like deep learning and PCA.
Jayita Gulati is a machine learning enthusiast and technical writer driven by her passion for building machine learning models. She holds a Master's degree in Computer Science from the University of Liverpool.