1. Đọc dữ liệu
data <- read.csv("C:/Users/Duyen/Downloads/dirty_data.csv")
knitr::kable(
  head(data, 5),
  caption = "Bảng dữ liệu 5 số đầu tiên"
)
Bảng dữ liệu 5 số đầu tiên
order_id customer_id date nearest_warehouse shopping_cart order_price delivery_charges customer_lat customer_long coupon_discount order_total season is_expedited_delivery distance_to_nearest_warehouse latest_customer_review is_happy_customer
ORD182494 ID6197211592 2019-06-22 Thompson [(‘Lucent 330S’, 1), (‘Thunder line’, 2), (‘iStream’, 2), (‘pearTV’, 1)] 12200 79.89 -37.81511 144.9328 10 11059.89 Winter True 1.2800 perfect phone and trusted seller. phone itself is amazing. i got this phone as shown here on the description. it came in with original packaging including all original accessories. yes this phone is unlocked, so no doubt about it. True
ORD395518 ID0282825849 2019-12-29 Thompson [(‘Thunder line’, 1), (‘Universe Note’, 2)] 9080 62.71 -37.80274 144.9511 0 9142.71 Summer False 1.1621 it keeps dropping calls the wifi don’t work this is a waste of money this phone is worthless, it keeps dropping calls the wifi don’t work this is a waste of money False
ORD494479 ID0579391891 2019-03-02 Nickolson [(‘Thunder line’, 1), (‘pearTV’, 2)] 10670 65.87 -37.82130 144.9576 10 9668.87 Autumn False 1.0949 five stars this is a great cheap phone. True
ORD019224 ID4544561904 2019-01-12 Nickolson [(‘Universe Note’, 1), (‘Alcon 10’, 2), (‘Olivia x460’, 1), (‘iAssist Line’, 1)] 24800 57.61 -37.81142 144.9731 15 21137.61 Summer False 0.8571 charger did not fit the charger didn’t fit. False
ORD104032 ID6231506320 2019-11-28 Nickolson [(‘Universe Note’, 1), (‘Olivia x460’, 1), (‘iStream’, 1), (‘Toshika 750’, 1)] 9145 75.54 37.82386 144.9699 25 6934.29 Spring False 0.5867 four stars good True

Cấu trúc tệp tin

str(data)
## 'data.frame':    500 obs. of  16 variables:
##  $ order_id                     : chr  "ORD182494" "ORD395518" "ORD494479" "ORD019224" ...
##  $ customer_id                  : chr  "ID6197211592" "ID0282825849" "ID0579391891" "ID4544561904" ...
##  $ date                         : chr  "2019-06-22" "2019-12-29" "2019-03-02" "2019-01-12" ...
##  $ nearest_warehouse            : chr  "Thompson" "Thompson" "Nickolson" "Nickolson" ...
##  $ shopping_cart                : chr  "[('Lucent 330S', 1), ('Thunder line', 2), ('iStream', 2), ('pearTV', 1)]" "[('Thunder line', 1), ('Universe Note', 2)]" "[('Thunder line', 1), ('pearTV', 2)]" "[('Universe Note', 1), ('Alcon 10', 2), ('Olivia x460', 1), ('iAssist Line', 1)]" ...
##  $ order_price                  : int  12200 9080 10670 24800 9145 7810 13700 7960 25390 13320 ...
##  $ delivery_charges             : num  79.9 62.7 65.9 57.6 75.5 ...
##  $ customer_lat                 : num  -37.8 -37.8 -37.8 -37.8 37.8 ...
##  $ customer_long                : num  145 145 145 145 145 ...
##  $ coupon_discount              : int  10 0 10 15 25 10 5 5 10 15 ...
##  $ order_total                  : num  11060 9143 9669 21138 6934 ...
##  $ season                       : chr  "Winter" "Summer" "Autumn" "Summer" ...
##  $ is_expedited_delivery        : chr  "True" "False" "False" "False" ...
##  $ distance_to_nearest_warehouse: num  1.28 1.162 1.095 0.857 0.587 ...
##  $ latest_customer_review       : chr  "perfect phone and trusted seller. phone itself is amazing. i got this phone as shown here on the description. i"| __truncated__ "it keeps dropping calls the wifi don't work this is a waste of money this phone is worthless, it keeps dropping"| __truncated__ "five stars this is a great cheap phone." "charger did not fit the charger didn't fit." ...
##  $ is_happy_customer            : chr  "True" "False" "True" "False" ...
  1. Làm sạch dữ liệu
new_data <- data[, c("nearest_warehouse", "delivery_charges", "coupon_discount", "order_total", "season", "is_expedited_delivery", "distance_to_nearest_warehouse", "is_happy_customer")]
head(new_data, 10)
##    nearest_warehouse delivery_charges coupon_discount order_total season
## 1           Thompson            79.89              10    11059.89 Winter
## 2           Thompson            62.71               0     9142.71 Summer
## 3          Nickolson            65.87              10     9668.87 Autumn
## 4          Nickolson            57.61              15    21137.61 Summer
## 5          Nickolson            75.54              25     6934.29 Spring
## 6             Bakers            71.22              10     7100.22 Spring
## 7           Thompson            74.84               5    13089.84 Spring
## 8           Thompson            52.28               5    10789.79 Autumn
## 9             Bakers           107.58              10    22958.58 Spring
## 10          Thompson            62.26              15    11384.26 winter
##    is_expedited_delivery distance_to_nearest_warehouse is_happy_customer
## 1                   True                        1.2800              True
## 2                  False                        1.1621             False
## 3                  False                        1.0949              True
## 4                  False                        0.8571             False
## 5                  False                        0.5867              True
## 6                  False                        2.0752             False
## 7                  False                        0.6767              True
## 8                  False                        1.3043             False
## 9                   True                        1.6595              True
## 10                  True                        0.6093             False
apply(is.na(new_data), 2, which)
## integer(0)
unique(new_data$nearest_warehouse)
## [1] "Thompson"  "Nickolson" "Bakers"    "nickolson" "thompson"

Kết quả chọn biến

knitr::kable(
  head(new_data, 10),
  caption = "Bảng dữ liệu sau khi lựa chọn biến"
)
Bảng dữ liệu sau khi lựa chọn biến
nearest_warehouse delivery_charges coupon_discount order_total season is_expedited_delivery distance_to_nearest_warehouse is_happy_customer
Thompson 79.89 10 11059.89 Winter True 1.2800 True
Thompson 62.71 0 9142.71 Summer False 1.1621 False
Nickolson 65.87 10 9668.87 Autumn False 1.0949 True
Nickolson 57.61 15 21137.61 Summer False 0.8571 False
Nickolson 75.54 25 6934.29 Spring False 0.5867 True
Bakers 71.22 10 7100.22 Spring False 2.0752 False
Thompson 74.84 5 13089.84 Spring False 0.6767 True
Thompson 52.28 5 10789.79 Autumn False 1.3043 False
Bakers 107.58 10 22958.58 Spring True 1.6595 True
Thompson 62.26 15 11384.26 winter True 0.6093 False

Kiểm tra nearest_house

new_data$nearest_warehouse[new_data$nearest_warehouse == 'thompson'] <- 'Thompson'
new_data$nearest_warehouse[new_data$nearest_warehouse == 'nickolson'] <- 'Nickolson'
unique(new_data$nearest_warehouse)
## [1] "Thompson"  "Nickolson" "Bakers"

Kiểm tra season

unique(new_data$season)
## [1] "Winter" "Summer" "Autumn" "Spring" "winter" "spring" "autumn" "summer"
new_data$season[new_data$season == 'spring'] <- 'Spring'
new_data$season[new_data$season == 'summer'] <- 'Summer'
new_data$season[new_data$season == 'autumn'] <- 'Autumn'
new_data$season[new_data$season == 'winter'] <- 'Winter'
unique(new_data$season)
## [1] "Winter" "Summer" "Autumn" "Spring"

Kiểm tra điều kiện tổng hóa đơn

new_data$calculated_total <- data$order_price*(1-data$coupon_discount/100) + data$delivery_charges
all.equal(data$order_total, new_data$calculated_total)
## [1] "Mean relative difference: 1.334735"
data$order_total <- new_data$calculated_total
all.equal(data$order_total, new_data$calculated_total)
## [1] TRUE