Đọc dữ liệu

data <- read.csv("C:/Users/Ngo Trang/Documents/Supermarket Transactions.csv", header = TRUE)
names(data)
##  [1] "X"                 "PurchaseDate"      "CustomerID"       
##  [4] "Gender"            "MaritalStatus"     "Homeowner"        
##  [7] "Children"          "AnnualIncome"      "City"             
## [10] "StateorProvince"   "Country"           "ProductFamily"    
## [13] "ProductDepartment" "ProductCategory"   "UnitsSold"        
## [16] "Revenue"

Chọn các biến định tính

bdt <- c("Gender","MaritalStatus","Homeowner", "City","StateorProvince", "Country","ProductFamily","ProductDepartment", "ProductCategory")

Tạo dữ liệu mới chỉ có biến định tính

d <- data[ ,bdt]

Lập bảng tần số cho biến “Country”

ntt1 <- table(data$Country)
ntt1
## 
## Canada Mexico    USA 
##    809   3688   9562

Vậy trong bộ dữ liệu này có 809 người ở Canada.

Vậy trong bộ dữ liệu này có 3688 người ở Mexico.

Vậy trong bộ dữ liệu này có 9562 người ở USA.

Lập bảng tần suất cho biến “Country”

ntt2 <- table(data$Country)/sum(nrow(data))
ntt2
## 
##     Canada     Mexico        USA 
## 0.05754321 0.26232307 0.68013372

Vậy trong bộ dữ liệu này có 5.7543211 % người ở Canada.

Vậy trong bộ dữ liệu này có 26.2323067 % người ở Mexico.

Vậy trong bộ dữ liệu này có 68.0133722 % người ở USA.

Vẽ biểu đồ cột thể hiện tần số cho biến “Country”

 text(barplot(ntt1, col = c("blue", "green", "red"),
             main = "Biểu đồ cột: Country",
             xlab = "quốc gia", ylab = "Tần số"),
     ntt1, labels = ntt1, pos = 1)

Vẽ biểu đồ tròn thể hiện tần suất cho biến “Country”

names(ntt2)
## [1] "Canada" "Mexico" "USA"
pie(ntt2,
    col = c("blue", "green","red"),
    main = "Pie chart with percentages",
    labels = ntt2)
    legend("topright", 
       legend = names(ntt2),
       fill = c("blue", "green", "red"),
       title = "Countries")