Advertisement
MCFoger

Untitled

Apr 13th, 2025
369
0
27 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.29 KB | None | 0 0
  1. library(dplyr)
  2. set.seed(123)
  3. transactions <- data.frame(
  4.  transaction_id = 1:1000,
  5.  user_id = sample(1:50, 1000, replace = TRUE),
  6.  amount = round(runif(1000, 1, 200), 2),
  7.  status = sample(c("success", "fail"), 1000, replace = TRUE, prob = c(0.8, 0.2)),
  8.  region_id = sample(1:5, 1000, replace = TRUE)
  9. )
  10.  
  11. users <- data.frame(
  12.  user_id = 1:50,
  13.  region_id = sample(1:5, 50, replace = TRUE)
  14. )
  15.  
  16. # Основний аналіз
  17. result <- transactions %>%
  18.  # Фільтруємо тільки успішні транзакції
  19.  filter(status == "success") %>%
  20.  group_by(user_id) %>%
  21.  summarise(total_spend = sum(amount)) %>%
  22.  left_join(users, by = "user_id") %>%
  23.  
  24.  group_by(region_id) %>%
  25.  mutate(avg_region_spend = mean(total_spend)) %>%
  26.  ungroup() %>%
  27.  
  28.  filter(total_spend > avg_region_spend) %>%
  29.  
  30.  arrange(region_id, desc(total_spend))
  31.  
  32. print(result)
  33.  
  34. library(ggplot2)
  35.  
  36. ggplot(result, aes(x = factor(region_id), y = total_spend)) +
  37.  geom_boxplot(aes(y = avg_region_spend), fill = "lightblue", alpha = 0.5) +
  38.  geom_point(aes(y = total_spend), color = "red", size = 3) +
  39.  labs(title = "Користувачі з витратами вище середнього по регіонах",
  40.       x = "Регіон",
  41.       y = "Сумарні витрати (TC)") +
  42.  theme_minimal()
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement