Last active
June 22, 2025 09:28
-
-
Save thinkphp/b592cd45850744c12aa4c4ba31d3fd6d to your computer and use it in GitHub Desktop.
probleme-solutii.R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PROBLEMA 1: Analiza stocurilor din magazine | |
# Calculează valoarea totală a stocului pentru fiecare produs din inventar (inclusiv produsele fără stoc), | |
# sortează descrescător și creează grafic barplot. | |
library(dplyr) | |
library(ggplot2) | |
# Unele produse din inventar nu au stoc înregistrat | |
stock_records <- data.frame( | |
product_id = c(101, 103, 105, 107, 101, 103), | |
warehouse = c("A", "A", "B", "A", "B", "C"), | |
quantity = c(50, 30, 25, 40, 35, 20), | |
unit_price = c(120, 85, 200, 95, 120, 85) | |
) | |
products_inventory <- data.frame( | |
product_id = c(101, 102, 103, 104, 105, 106, 107, 108), | |
product_name = c("Laptop", "Mouse", "Tastură", "Monitor", "Tabletă", "Căști", "Telefon", "Cameră"), | |
category = c("IT", "Accesorii", "Accesorii", "IT", "IT", "Audio", "Mobile", "Foto") | |
) | |
# RIGHT JOIN pentru a include toate produsele din inventar | |
stock_analysis <- stock_records %>% | |
right_join(products_inventory, by = "product_id") %>% | |
mutate( | |
quantity = ifelse(is.na(quantity), 0, quantity), | |
unit_price = ifelse(is.na(unit_price), 0, unit_price) | |
) %>% | |
group_by(product_name) %>% | |
summarise(total_value = sum(quantity * unit_price)) %>% | |
arrange(desc(total_value)) | |
print(stock_analysis) | |
# Grafic barplot | |
ggplot(stock_analysis, aes(x = reorder(product_name, -total_value), y = total_value)) + | |
geom_col(fill = "darkblue") + | |
labs(title = "Valoarea Totală a Stocului pe Produs", x = "Produs", y = "Valoare Totală (RON)") + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) + | |
scale_y_continuous(labels = scales::comma) | |
# =============================================================================== | |
# PROBLEMA 2: Analiza performanței echipelor sportive | |
# Calculează punctajul mediu pentru fiecare echipă înregistrată (inclusiv echipele care nu au jucat), | |
# sortează descrescător și afișează grafic. | |
match_results <- data.frame( | |
team_id = c(1, 2, 4, 5, 1, 2, 4), | |
opponent = c("Steaua", "Dinamo", "Rapid", "CFR", "Dinamo", "Rapid", "Steaua"), | |
points = c(3, 1, 0, 3, 1, 3, 1) | |
) | |
registered_teams <- data.frame( | |
team_id = 1:6, | |
team_name = c("Universitatea", "Petrolul", "Voluntari", "Sepsi", "Hermannstadt", "Botoșani"), | |
city = c("Cluj", "Ploiești", "Voluntari", "Sfântu Gheorghe", "Sibiu", "Botoșani") | |
) | |
# RIGHT JOIN pentru toate echipele înregistrate | |
team_performance <- match_results %>% | |
right_join(registered_teams, by = "team_id") %>% | |
mutate(points = ifelse(is.na(points), 0, points)) %>% | |
group_by(team_name) %>% | |
summarise(avg_points = mean(points)) %>% | |
arrange(desc(avg_points)) | |
print(team_performance) | |
# Grafic barplot | |
ggplot(team_performance, aes(x = reorder(team_name, -avg_points), y = avg_points)) + | |
geom_col(fill = "forestgreen") + | |
labs(title = "Punctajul Mediu pe Echipă", x = "Echipă", y = "Puncte Medii") + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | |
# =============================================================================== | |
# PROBLEMA 3: Analiza comenzilor clienților | |
# Calculează suma totală a comenzilor pentru fiecare client din baza de date (inclusiv cei fără comenzi), | |
# sortează descrescător și creează grafic. | |
orders <- data.frame( | |
customer_id = c(2, 3, 5, 7, 2, 3, 5), | |
order_date = as.Date(c("2024-01-15", "2024-01-20", "2024-02-01", "2024-02-05", "2024-02-10", "2024-02-15", "2024-03-01")), | |
amount = c(250, 180, 320, 150, 200, 280, 400) | |
) | |
customers_database <- data.frame( | |
customer_id = 1:8, | |
customer_name = c("Andreea Pop", "Bogdan Marin", "Carmen Stoica", "Dan Petrescu", | |
"Elena Radu", "Florin Dima", "Gabriela Ionescu", "Horia Văcaru"), | |
membership = c("Gold", "Silver", "Bronze", "Gold", "Silver", "Bronze", "Gold", "Silver") | |
) | |
# RIGHT JOIN pentru toți clienții din baza de date | |
customer_orders <- orders %>% | |
right_join(customers_database, by = "customer_id") %>% | |
mutate(amount = ifelse(is.na(amount), 0, amount)) %>% | |
group_by(customer_name) %>% | |
summarise(total_orders = sum(amount)) %>% | |
arrange(desc(total_orders)) | |
print(customer_orders) | |
# Grafic barplot | |
ggplot(customer_orders, aes(x = reorder(customer_name, -total_orders), y = total_orders)) + | |
geom_col(fill = "coral") + | |
labs(title = "Suma Totală Comenzi pe Client", x = "Client", y = "Total Comenzi (RON)") + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | |
# =============================================================================== | |
# PROBLEMA 4: Analiza prezenței la cursuri | |
# Calculează rata medie de prezență pentru fiecare curs programat (inclusiv cursurile fără prezențe înregistrate), | |
# sortează descrescător și afișează grafic. | |
attendance_records <- data.frame( | |
course_id = c(201, 202, 204, 206, 201, 202, 204), | |
session_date = as.Date(c("2024-03-01", "2024-03-01", "2024-03-02", "2024-03-03", | |
"2024-03-08", "2024-03-08", "2024-03-09")), | |
students_present = c(18, 22, 15, 25, 20, 24, 17), | |
total_enrolled = c(25, 28, 20, 30, 25, 28, 20) | |
) | |
scheduled_courses <- data.frame( | |
course_id = c(201, 202, 203, 204, 205, 206), | |
course_title = c("Algoritmi", "Baze de Date", "Rețele", "Programare Web", "Inteligență Artificială", "Securitate IT"), | |
instructor = c("Prof. Andrei", "Prof. Maria", "Prof. Ionescu", "Prof. Georgescu", "Prof. Radu", "Prof. Elena"), | |
credits = c(6, 5, 4, 6, 5, 4) | |
) | |
# RIGHT JOIN pentru toate cursurile programate | |
course_attendance <- attendance_records %>% | |
right_join(scheduled_courses, by = "course_id") %>% | |
mutate( | |
attendance_rate = ifelse(is.na(students_present), 0, students_present / total_enrolled * 100) | |
) %>% | |
group_by(course_title) %>% | |
summarise(avg_attendance = mean(attendance_rate)) %>% | |
arrange(desc(avg_attendance)) | |
print(course_attendance) | |
# Grafic barplot | |
ggplot(course_attendance, aes(x = reorder(course_title, -avg_attendance), y = avg_attendance)) + | |
geom_col(fill = "steelblue") + | |
labs(title = "Rata Medie de Prezență pe Curs", x = "Curs", y = "Prezență Medie (%)") + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) + | |
ylim(0, 100) | |
# =============================================================================== | |
# PROBLEMA 5: Analiza vânzărilor pe regiuni | |
# Calculează venitul total pentru fiecare regiune de vânzare (inclusiv regiunile fără vânzări), | |
# sortează descrescător și creează grafic. | |
sales_data <- data.frame( | |
region_id = c(1, 2, 4, 6, 1, 2, 4, 6), | |
sale_date = as.Date(c("2024-01-10", "2024-01-15", "2024-01-20", "2024-02-01", | |
"2024-02-05", "2024-02-10", "2024-02-15", "2024-02-20")), | |
revenue = c(15000, 12000, 8000, 18000, 16000, 14000, 9000, 20000) | |
) | |
sales_regions <- data.frame( | |
region_id = 1:7, | |
region_name = c("Nord", "Sud", "Est", "Vest", "Centru", "Nord-Est", "Sud-Vest"), | |
manager = c("Ana Popescu", "Ion Marinescu", "Maria Georgescu", "Petru Ionescu", | |
"Elena Stoica", "Mihai Dumitrescu", "Sofia Radu"), | |
target = c(50000, 45000, 35000, 40000, 30000, 25000, 38000) | |
) | |
# RIGHT JOIN pentru toate regiunile de vânzare | |
regional_sales <- sales_data %>% | |
right_join(sales_regions, by = "region_id") %>% | |
mutate(revenue = ifelse(is.na(revenue), 0, revenue)) %>% | |
group_by(region_name) %>% | |
summarise(total_revenue = sum(revenue)) %>% | |
arrange(desc(total_revenue)) | |
print(regional_sales) | |
# Grafic barplot | |
ggplot(regional_sales, aes(x = reorder(region_name, -total_revenue), y = total_revenue)) + | |
geom_col(fill = "darkorange") + | |
labs(title = "Venitul Total pe Regiune de Vânzare", x = "Regiune", y = "Venit Total (RON)") + | |
scale_y_continuous(labels = scales::comma) + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment