d <- read.csv("D:/PTDLDT/Supermarket Transactions.csv")
dldt <- c("Gender", "MaritalStatus", "Homeowner","City", "StateorProvince", "Country", "ProductFamily", "ProductDepartment", "ProductCategory")
dldt
## [1] "Gender" "MaritalStatus" "Homeowner"
## [4] "City" "StateorProvince" "Country"
## [7] "ProductFamily" "ProductDepartment" "ProductCategory"
dt <- d[, dldt]
head(dt)
## Gender MaritalStatus Homeowner City StateorProvince Country
## 1 F S Y Los Angeles CA USA
## 2 M M Y Los Angeles CA USA
## 3 F M N Bremerton WA USA
## 4 M M Y Portland OR USA
## 5 F S Y Beverly Hills CA USA
## 6 F M Y Beverly Hills CA USA
## ProductFamily ProductDepartment ProductCategory
## 1 Food Snack Foods Snack Foods
## 2 Food Produce Vegetables
## 3 Food Snack Foods Snack Foods
## 4 Food Snacks Candy
## 5 Drink Beverages Carbonated Beverages
## 6 Food Deli Side Dishes
#Lập bảng tần suất của biến Gender
table(dt$Gender)/sum(nrow(dt))
##
## F M
## 0.5099936 0.4900064
#Lập bảng tần số biến Gender
table(dt$Gender)
##
## F M
## 7170 6889
Biểu đồ
# Bảng tần suất và tỷ lệ %
gender_freq <- table(dt$Gender)
gender_pct <- gender_freq / nrow(dt)
# Vẽ biểu đồ tròn
pie(gender_freq,
main = "Phân bố giới tính (Gender)",
labels = paste0(names(gender_freq),
" (", round(gender_pct*100, 1), "%)"),
col = c("lightpink", "green"))
Vậy trong bộ dữ liệu này có 50.9993598 % nữ và 49.0006402% nam.
#Lập bảng tần suất của biến Homeowner
table(dt$Homeowner)/sum(nrow(dt))
##
## N Y
## 0.3993883 0.6006117
#Lập bảng tần số biến Homeowner
table(dt$Homeowner)
##
## N Y
## 5615 8444
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
ggplot(dt, aes(x = Homeowner)) +
geom_bar(fill = "red") +
theme_minimal() +
labs(title = "Biểu đồ tần suất theo Người có nhà hay không", x = "Nhà riêng", y = "Tần suất")
Vậy trong bộ dữ liệu này có 39.9388292 % không có nhà và 60.0611708% có nhà.
#Lập bảng tần suất của biến MaritalStatus
table(dt$MaritalStatus)/sum(nrow(dt))
##
## M S
## 0.4883704 0.5116296
#Lập bảng tần số của biến MaritalStatus
table(dt$MaritalStatus)
##
## M S
## 6866 7193
library(ggplot2)
ggplot(dt, aes(x = MaritalStatus)) +
geom_bar(fill = "steelblue") +
theme_minimal() +
labs(title = "Biểu đồ tần suất theo Trạng thái hôn nhân", x = "Trạng thái", y = "Tần suất")
Vậy trong bộ dữ liệu này có 48.8370439 % đã kết hôn và 51.1629561% độc thân.
#Lập bảng tần suất của biến City
table(dt$City)/sum(nrow(dt))
##
## Acapulco Bellingham Beverly Hills Bremerton Camacho
## 0.027242336 0.010171420 0.057685468 0.059321431 0.032150224
## Guadalajara Hidalgo Los Angeles Merida Mexico City
## 0.005334661 0.060103848 0.065865282 0.046518245 0.013798990
## Orizaba Portland Salem San Andres San Diego
## 0.033003770 0.062308841 0.098584537 0.044170994 0.061597553
## San Francisco Seattle Spokane Tacoma Vancouver
## 0.009246746 0.065580767 0.062237712 0.089408920 0.045024539
## Victoria Walla Walla Yakima
## 0.012518671 0.011380610 0.026744434
#Lập bảng tần số của biến City
table(dt$City)
##
## Acapulco Bellingham Beverly Hills Bremerton Camacho
## 383 143 811 834 452
## Guadalajara Hidalgo Los Angeles Merida Mexico City
## 75 845 926 654 194
## Orizaba Portland Salem San Andres San Diego
## 464 876 1386 621 866
## San Francisco Seattle Spokane Tacoma Vancouver
## 130 922 875 1257 633
## Victoria Walla Walla Yakima
## 176 160 376
ggplot(dt, aes(x = City)) +
geom_bar() +
coord_flip() + # xoay ngang cho gọn
labs(title = "Tần suất giao dịch theo City",
x = "City", y = "Số giao dịch")
Trong bộ dữ liệu này, thành phố có tỷ lệ giao dịch cao nhất là Salem với khoảng 9.86%, tiếp theo là Tacoma (8.94%), Los Angeles (6.59%) và Seattle (6.56%).
Các thành phố nhỏ hơn như Hidalgo (6.01%) hoặc Guadalajara (0.53%) có tỷ lệ thấp hơn nhiều.
Dữ liệu này cho thấy các giao dịch chủ yếu diễn ra tại một số thành phố lớn – nơi có mật độ dân cư cao hoặc hoạt động kinh tế sôi động hơn. Ngược lại, các thành phố nhỏ đóng góp tỷ trọng giao dịch thấp hơn, có thể do thị trường hạn chế về quy mô hoặc sản phẩm chưa được biết đến rộng rãi.
Bên cạnh đó, xu hướng phân bố này cũng phần nào phản ánh chiến lược tiếp thị và định hướng kênh phân phối của siêu thị, với trọng tâm đặt vào các khu vực đô thị lớn.
#Lập bảng tần suất của biến StateorProvince
table(dt$StateorProvince)/sum(nrow(dt))
##
## BC CA DF Guerrero Jalisco OR
## 0.057543211 0.194395049 0.057969984 0.027242336 0.005334661 0.160893378
## Veracruz WA Yucatan Zacatecas
## 0.033003770 0.324845295 0.046518245 0.092254072
#Lập bảng tần số của biến StateorProvince
table(dt$StateorProvince)
##
## BC CA DF Guerrero Jalisco OR Veracruz WA
## 809 2733 815 383 75 2262 464 4567
## Yucatan Zacatecas
## 654 1297
ggplot(dt, aes(x = StateorProvince)) +
geom_bar(fill = "orange") +
coord_flip() +
labs(title = "Tần suất giao dịch theo Tiểu bang hoặc tỉnh",
x = "StateorProvince", y = "Số giao dịch")
Trong bộ dữ liệu này, bang hoặc tỉnh có tỷ lệ giao dịch lớn nhất là WA với khoảng 32.48%, tiếp theo là CA (19.44%) và OR (16.09%). Các bang như Jalisco (0.53%) và Guerrero (2.72%) chiếm tỷ lệ thấp hơn. Từ đó, ta kết luận được rằng dữ liệu cho thấy sự tập trung giao dịch cao ở một số bang chính.
#Lập bảng tần suất của biến Country
table(dt$Country)/sum(nrow(dt))
##
## Canada Mexico USA
## 0.05754321 0.26232307 0.68013372
#Lập bảng tần suất của biến Country
table(dt$Country)
##
## Canada Mexico USA
## 809 3688 9562
# Bảng tần suất và tỷ lệ %
gender_freq <- table(dt$ Country)
gender_pct <- gender_freq / nrow(dt)
# Vẽ biểu đồ tròn
pie(gender_freq,
main = "Phân bố Country",
labels = paste0(names(gender_freq),
" (", round(gender_pct*100, 1), "%)"),
col = c("lightpink", "green", "yellow"))
Vậy trong bộ dữ liệu này có 5.7543211 % ở Canada, 26.2323067% ở Mexico và 68.0133722% ở USA.
#Lập bảng tần suất của biến ProductFamily
table(dt$ProductFamily)/sum(nrow(dt))
##
## Drink Food Non-Consumable
## 0.08891102 0.72217085 0.18891813
#Lập bảng tần suất của biến ProductFamily
table(dt$ProductFamily)
##
## Drink Food Non-Consumable
## 1250 10153 2656
library(ggplot2)
ggplot(dt, aes(x = ProductFamily)) +
geom_bar(fill = "pink") +
theme_minimal() +
labs(title = "Biểu đồ tần suất theo Sản phẩm gia đình", x = "Sản phẩm", y = "Tần suất")
Vậy trong bộ dữ liệu này có 8.8911018 % đồ uống, 72.2170851% thức ăn và 18.8918131% không tiêu thụ được.
#Lập bảng tần suất của biến ProductDepartment
table(dt$ProductDepartment)/sum(nrow(dt))
##
## Alcoholic Beverages Baked Goods Baking Goods Beverages
## 0.025321858 0.030229746 0.076250089 0.048367594
## Breakfast Foods Canned Foods Canned Products Carousel
## 0.013372217 0.069492852 0.007753041 0.004196600
## Checkout Dairy Deli Eggs
## 0.005832563 0.064229319 0.049719041 0.014083505
## Frozen Foods Health and Hygiene Household Meat
## 0.098300021 0.063518031 0.101002916 0.006330464
## Periodicals Produce Seafood Snack Foods
## 0.014368020 0.141830856 0.007255139 0.113806103
## Snacks Starchy Foods
## 0.025037343 0.019702682
#Lập bảng tần suất của biến ProductDepartment
table(dt$ProductDepartment)
##
## Alcoholic Beverages Baked Goods Baking Goods Beverages
## 356 425 1072 680
## Breakfast Foods Canned Foods Canned Products Carousel
## 188 977 109 59
## Checkout Dairy Deli Eggs
## 82 903 699 198
## Frozen Foods Health and Hygiene Household Meat
## 1382 893 1420 89
## Periodicals Produce Seafood Snack Foods
## 202 1994 102 1600
## Snacks Starchy Foods
## 352 277
ggplot(dt, aes(x = ProductDepartment)) +
geom_bar() +
coord_flip() + # xoay ngang cho gọn
labs(title = "Tần suất giao dịch theo ProductDepartment",
x = "ProductDepartment", y = "Số giao dịch")
Trong bộ dữ liệu, nhóm sản phẩm có tỷ lệ giao dịch cao nhất là Produce với 14.18%, tiếp theo là Snack Foods (11.38%) và Household (10.1%). Nhóm Frozen Foods cũng chiếm tỷ lệ đáng kể là 9.83%. Các nhóm như Carousel (0.42%) và Checkout (0.58%) có tỷ lệ thấp hơn nhiều.
#Lập bảng tần suất của biến ProductCategory
table(dt$ProductCategory)/sum(nrow(dt))
##
## Baking Goods Bathroom Products Beer and Wine
## 0.034426346 0.025962017 0.025321858
## Bread Breakfast Foods Candles
## 0.030229746 0.029660716 0.003200797
## Candy Canned Anchovies Canned Clams
## 0.025037343 0.003129668 0.003769827
## Canned Oysters Canned Sardines Canned Shrimp
## 0.002489508 0.002845153 0.002702895
## Canned Soup Canned Tuna Carbonated Beverages
## 0.028736041 0.006188207 0.010953837
## Cleaning Supplies Cold Remedies Dairy
## 0.013443346 0.006614980 0.064229319
## Decongestants Drinks Eggs
## 0.006045949 0.009602390 0.014083505
## Electrical Frozen Desserts Frozen Entrees
## 0.025250729 0.022974607 0.008393200
## Fruit Hardware Hot Beverages
## 0.054413543 0.009175617 0.016075112
## Hygiene Jams and Jellies Kitchen Products
## 0.014012376 0.041823743 0.015434953
## Magazines Meat Miscellaneous
## 0.014368020 0.054129028 0.002987410
## Packaged Vegetables Pain Relievers Paper Products
## 0.003414183 0.013656732 0.024539441
## Pizza Plastic Products Pure Juice Beverages
## 0.013798990 0.010029163 0.011736254
## Seafood Side Dishes Snack Foods
## 0.007255139 0.010882709 0.113806103
## Specialty Starchy Foods Vegetables
## 0.020556227 0.019702682 0.122910591
#Lập bảng tần suất của biến ProductCategory
table(dt$ProductCategory)
##
## Baking Goods Bathroom Products Beer and Wine
## 484 365 356
## Bread Breakfast Foods Candles
## 425 417 45
## Candy Canned Anchovies Canned Clams
## 352 44 53
## Canned Oysters Canned Sardines Canned Shrimp
## 35 40 38
## Canned Soup Canned Tuna Carbonated Beverages
## 404 87 154
## Cleaning Supplies Cold Remedies Dairy
## 189 93 903
## Decongestants Drinks Eggs
## 85 135 198
## Electrical Frozen Desserts Frozen Entrees
## 355 323 118
## Fruit Hardware Hot Beverages
## 765 129 226
## Hygiene Jams and Jellies Kitchen Products
## 197 588 217
## Magazines Meat Miscellaneous
## 202 761 42
## Packaged Vegetables Pain Relievers Paper Products
## 48 192 345
## Pizza Plastic Products Pure Juice Beverages
## 194 141 165
## Seafood Side Dishes Snack Foods
## 102 153 1600
## Specialty Starchy Foods Vegetables
## 289 277 1728
ggplot(dt, aes(x = ProductCategory, fill = ProductCategory)) +
geom_bar() +
coord_flip() + # xoay ngang cho gọn
labs(title = "Tần suất giao dịch theo ProductCategory",
x = "ProductCategory", y = "Số giao dịch") +
theme(legend.position = "none") # ẩn chú thích nếu muốn
Trong bộ dữ liệu, nhóm sản phẩm có tỷ lệ giao dịch cao nhất là Vegetables với 12.29%, tiếp theo là Snack Foods (11.38%) và Dairy (6.42%). Các nhóm đóng hộp như Canned Oysters (0.25%) và Canned Clams (0.38%) chiếm tỷ lệ rất nhỏ, cho thấy sự ưu tiên mua hàng tươi và đồ ăn nhẹ.