
La segmentación o clusters es un conjunto de técnicas cuyo propósito
es formar grupos a partir de un conjunto de elementos.
Paso 1. Instalar paquetes y llamar librerías
library(cluster)
library(ggplot2)
library(data.table)
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
spotify <- read.csv("~/Desktop/Most Streamed Spotify Songs 2024.csv")
# Paso 3. Cantidad de grupos
grupos <- 3
# Paso 4. Generar los segmentos
segmentos <- kmeans(df,4)
segmentos
## K-means clustering with 4 clusters of sizes 3, 2, 1, 2
##
## Cluster means:
## x y
## 1 3.666667 9.0
## 2 7.500000 4.5
## 3 6.000000 4.0
## 4 1.500000 3.5
##
## Clustering vector:
## [1] 1 4 2 1 2 3 4 1
##
## Within cluster sum of squares by cluster:
## [1] 6.666667 1.000000 0.000000 5.000000
## (between_SS / total_SS = 87.4 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
# Paso 5. Asignar el grupo al que pertenece cada observación
asignacion <- cbind(df, cluster = segmentos$cluster)
asignacion
## x y cluster
## 1 2 10 1
## 2 2 5 4
## 3 8 4 2
## 4 5 8 1
## 5 7 5 2
## 6 6 4 3
## 7 1 2 4
## 8 4 9 1
# Paso 6. Graficar los clusters
library(factoextra)
fviz_cluster(segmentos, data=df)

# Paso 7. Optimizar la cantidad de grupos
# La cantidad óptima de grupos corresponde al punto
# más alto de la siguiente gráfica.
library(cluster)
set.seed(123)
optimizacion <- clusGap(df, FUN=kmeans, nstart=1, K.max =7)
plot(optimizacion, xlab="Número de clusters k")

Conclusión
La segmentación o clusters es un algoritmo útil para las
empresas
que desean clasificar sus clientes y dirigir campañas de
marketing
más enfocadas y especializadas.