Clustering is a powerful technique for grouping similar data points. We’ll use the k-means algorithm.
Step 1: Create a Sample Dataset
rCopy code# Generate a sample dataset
set.seed(111)
cluster_data <- data.frame(
x = rnorm(100),
y = rnorm(100)
)
# Visualize the data
plot(cluster_data$x, cluster_data$y, main = "Sample Data for Clustering", xlab = "X", ylab = "Y")
Step 2: Apply k-means Clustering
rCopy code# Apply k-means clustering
kmeans_result <- kmeans(cluster_data, centers = 3)
# Add the cluster assignments to the dataset
cluster_data$cluster <- as.factor(kmeans_result$cluster)
# Plot the clusters
library(ggplot2)
ggplot(cluster_data, aes(x = x, y = y, color = cluster)) +
geom_point() +
labs(title = "K-means Clustering Result") +
theme_minimal()
Leave a Reply