d <- read.csv(file.choose(), header =T)
bdtinh <- c("MaritalStatus","City","Gender","Homeowner","StateorProvince","Country","ProductFamily","ProductDepartment","ProductCategory")
dc <- d[, bdtinh]
head(dc)
## MaritalStatus City Gender Homeowner StateorProvince Country
## 1 S Los Angeles F Y CA USA
## 2 M Los Angeles M Y CA USA
## 3 M Bremerton F N WA USA
## 4 M Portland M Y OR USA
## 5 S Beverly Hills F Y CA USA
## 6 M Beverly Hills F Y 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
table(d$Homeowner)
##
## N Y
## 5615 8444
tmp <- table(d$Homeowner)/sum(nrow(d))
tmp
##
## N Y
## 0.3993883 0.6006117
Vậy trong bộ dữ liệu này có 39.9388292 % không phải là chủ nhà và 60.0611708 % là chủ nhà.
## Vẽ biểu đồ hình tròn (Pie Chart)
# Tính tỷ lệ %
labels <- paste(names(tmp), " - ", round(tmp * 100, 2), "%", sep = "")
# Vẽ pie chart
pie(tmp, labels = labels, main = "Tỷ lệ khách hàng theo tình trạng sở hữu nhà",
col = c("#FF7F0E", "#1F78B4"))
## Vẽ biểu đồ cột (Bar Chart)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.2
# Tạo bảng tần số
freq_table <- table(d$Homeowner)
# Chuyển thành data frame
df_freq <- as.data.frame(freq_table)
colnames(df_freq) <- c("Homeowner", "Frequency")
# Sắp xếp theo thứ tự Y/N nếu cần
df_freq$Homeowner <- factor(df_freq$Homeowner, levels = c("N", "Y"))
# Vẽ biểu đồ cột
ggplot(df_freq, aes(x = Homeowner, y = Frequency, fill = Homeowner)) +
geom_bar(stat = "identity") +
labs(title = "Số lượng khách hàng theo tình trạng sở hữu nhà",
x = "Tình trạng sở hữu nhà (Y: Có, N: Không)",
y = "Tần số") +
theme_minimal() +
scale_fill_manual(values = c("#FF7F0E", "#1F78B4"))
table(d$MaritalStatus)
##
## M S
## 6866 7193
tmp1 <- table(d$MaritalStatus)/sum(nrow(d))
tmp1
##
## M S
## 0.4883704 0.5116296
Vậy trong bộ dữ liệu này có 48.8370439 % đã kết hôn và 51.1629561 % là độc thân.
## Vẽ biểu đồ hình tròn (Pie Chart)
# Tạo nhãn phần trăm
labels <- paste(names(tmp1), " (", round(tmp1 * 100, 2), "%)", sep = "")
# Vẽ pie chart
pie(tmp1, labels = labels, main = "Tỷ lệ phần trăm theo tình trạng hôn nhân",
col = rainbow(length(tmp1)))
## Vẽ biểu đồ cột (Bar Chart)
# Tạo bảng tần số
freq_table <- table(d$MaritalStatus)
# Chuyển thành data frame để vẽ ggplot
freq_df <- as.data.frame(freq_table)
colnames(freq_df) <- c("MaritalStatus", "Frequency")
# Sắp xếp theo tần số giảm dần
freq_df <- freq_df[order(-freq_df$Frequency), ]
# Vẽ biểu đồ cột
ggplot(freq_df, aes(x = reorder(MaritalStatus, -Frequency), y = Frequency, fill = MaritalStatus)) +
geom_bar(stat = "identity") +
labs(title = "Tần số theo tình trạng hôn nhân",
x = "Tình trạng hôn nhân",
y = "Tần số") +
theme_minimal() +
scale_fill_brewer(palette = "Set3")
table(d$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
tmp2 <- table(d$City)/sum(nrow(d))
tmp2
##
## 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
## Vẽ biểu đồ hình tròn (Pie Chart)
# Tạo nhãn có phần trăm
labels <- paste(names(tmp2), " (", round(tmp2 * 100, 2), "%)", sep = "")
# Vẽ biểu đồ hình tròn
pie(tmp2, labels = labels, main = "Tỷ lệ % theo Thành phố",
col = rainbow(length(tmp2)))
## Vẽ biểu đồ cột (Bar Chart)
library(ggplot2)
# Tạo bảng tần số
city_freq <- table(d$City)
# Chuyển thành data frame
city_df <- as.data.frame(city_freq)
colnames(city_df) <- c("City", "Frequency")
# Sắp xếp theo tần số giảm dần
city_df <- city_df[order(-city_df$Frequency), ]
# Vẽ biểu đồ cột
ggplot(city_df, aes(x = reorder(City, -Frequency), y = Frequency)) +
geom_bar(stat = "identity", fill = "steelblue") +
labs(title = "Tần số theo Thành phố",
x = "Thành phố",
y = "Tần số") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
table(d$Gender)
##
## F M
## 7170 6889
tmp3 <- table(d$Gender)/sum(nrow(d))
tmp3
##
## F M
## 0.5099936 0.4900064
Vậy trong bộ dữ liệu này có 50.9993598 % là nữ và 51.1629561 % là nam.
## Vẽ biểu đồ hình tròn (Pie Chart)
# Tạo nhãn hiển thị %
labels <- paste(names(tmp3), "\n(", tmp3 * 100, "%)", sep = "")
pie(tmp3, labels = labels, main = "Tỷ lệ giới tính trong bộ dữ liệu",
col = c("lightblue", "salmon"))
## Vẽ biểu đồ cột (Bar Chart)
# Chuyển dữ liệu thành data frame để vẽ đồ thị
gender_df <- as.data.frame(table(d$Gender))
colnames(gender_df) <- c("Gender", "Frequency")
# Vẽ biểu đồ cột
ggplot(gender_df, aes(x = Gender, y = Frequency, fill = Gender)) +
geom_bar(stat = "identity") +
labs(title = "Tần số theo Giới tính",
x = "Giới tính",
y = "Tần số") +
theme_minimal()
table(d$StateorProvince)
##
## BC CA DF Guerrero Jalisco OR Veracruz WA
## 809 2733 815 383 75 2262 464 4567
## Yucatan Zacatecas
## 654 1297
tmp3 <- table(d$Gender)/sum(nrow(d))
tmp3
##
## F M
## 0.5099936 0.4900064
Vậy trong bộ dữ liệu này có 50.9993598 % là nữ và 51.1629561 % là nam.
## Vẽ biểu đồ hình tròn (Pie Chart)
# Tạo nhãn hiển thị %
labels <- paste(names(tmp3), "\n(", tmp3 * 100, "%)", sep = "")
pie(tmp3, labels = labels, main = "Tỷ lệ giới tính trong bộ dữ liệu",
col = c("lightblue", "salmon"))
## Vẽ biểu đồ cột (Bar Chart)
# Chuyển dữ liệu thành data frame để vẽ đồ thị
gender_df <- as.data.frame(table(d$Gender))
colnames(gender_df) <- c("Gender", "Frequency")
# Vẽ biểu đồ cột
ggplot(gender_df, aes(x = Gender, y = Frequency, fill = Gender)) +
geom_bar(stat = "identity") +
labs(title = "Tần số theo Giới tính",
x = "Giới tính",
y = "Tần số") +
theme_minimal()