library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)   
data <- read.csv("C:\\Users\\HP_VICTUS\\OneDrive\\Desktop\\Mall_Customers.csv")
str(data)
## 'data.frame':    200 obs. of  5 variables:
##  $ CustomerID            : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Gender                : chr  "Male" "Male" "Female" "Female" ...
##  $ Age                   : int  19 21 20 23 31 22 35 23 64 30 ...
##  $ Annual.Income..k..    : int  15 15 16 16 17 17 18 18 19 19 ...
##  $ Spending.Score..1.100.: int  39 81 6 77 40 76 6 94 3 72 ...
summary(data)
##    CustomerID        Gender               Age        Annual.Income..k..
##  Min.   :  1.00   Length:200         Min.   :18.00   Min.   : 15.00    
##  1st Qu.: 50.75   Class :character   1st Qu.:28.75   1st Qu.: 41.50    
##  Median :100.50   Mode  :character   Median :36.00   Median : 61.50    
##  Mean   :100.50                      Mean   :38.85   Mean   : 60.56    
##  3rd Qu.:150.25                      3rd Qu.:49.00   3rd Qu.: 78.00    
##  Max.   :200.00                      Max.   :70.00   Max.   :137.00    
##  Spending.Score..1.100.
##  Min.   : 1.00         
##  1st Qu.:34.75         
##  Median :50.00         
##  Mean   :50.20         
##  3rd Qu.:73.00         
##  Max.   :99.00
head(data)
##   CustomerID Gender Age Annual.Income..k.. Spending.Score..1.100.
## 1          1   Male  19                 15                     39
## 2          2   Male  21                 15                     81
## 3          3 Female  20                 16                      6
## 4          4 Female  23                 16                     77
## 5          5 Female  31                 17                     40
## 6          6 Female  22                 17                     76
sum(is.na(data))
## [1] 0
data$Gender <- as.factor(data$Gender)
gender_plot <- ggplot(data, aes(x = Gender)) + 
  geom_bar(fill = "steelblue") +
  theme_minimal() +
  labs(title = "Gender Distribution")
age_plot <- ggplot(data, aes(x = Age)) + 
  geom_histogram(bins = 20, fill = "skyblue", color = "black") +
  theme_minimal() +
  labs(title = "Age Distribution", x = "Age", y = "Frequency")
income_plot <- ggplot(data, aes(x = Annual.Income..k..)) + 
  geom_histogram(bins = 20, fill = "lightgreen", color = "black") +
  theme_minimal() +
  labs(title = "Annual Income Distribution", x = "Annual Income (k$)", y = "Frequency")
spending_plot <- ggplot(data, aes(x = Spending.Score..1.100.)) + 
  geom_histogram(bins = 20, fill = "lightcoral", color = "black") +
  theme_minimal() +
  labs(title = "Spending Score Distribution", x = "Spending Score (1-100)", y = "Frequency")
print(gender_plot)

print(age_plot)

print(income_plot)

print(spending_plot)

age_spending_plot <- ggplot(data, aes(x = Age, y = Spending.Score..1.100.)) + 
  geom_point(color = "blue") +
  theme_minimal() +
  labs(title = "Age vs Spending Score", x = "Age", y = "Spending Score")
income_spending_plot <- ggplot(data, aes(x = Annual.Income..k.., y = Spending.Score..1.100.)) + 
  geom_point(color = "purple") +
  theme_minimal() +
  labs(title = "Annual Income vs Spending Score", x = "Annual Income (k$)", y = "Spending Score")
print(age_spending_plot)

print(income_spending_plot)

numeric_data <- data %>% select(Age, Annual.Income..k.., Spending.Score..1.100.)
cor_matrix <- cor(numeric_data)
print("Correlation Matrix:")
## [1] "Correlation Matrix:"
print(cor_matrix)
##                                Age Annual.Income..k.. Spending.Score..1.100.
## Age                     1.00000000       -0.012398043           -0.327226846
## Annual.Income..k..     -0.01239804        1.000000000            0.009902848
## Spending.Score..1.100. -0.32722685        0.009902848            1.000000000
set.seed(123)
kmeans_result <- kmeans(numeric_data, centers = 3)
data$Cluster <- as.factor(kmeans_result$cluster)
cluster_plot <- ggplot(data, aes(x = Annual.Income..k.., y = Spending.Score..1.100., color = Cluster)) + 
  geom_point() +
  theme_minimal() +
  labs(title = "Customer Segmentation (K-Means Clustering)", x = "Annual Income (k$)", y = "Spending Score")

print(cluster_plot)