Data preprocessing/processing is an important step in the data mining process. It refers to the cleaning, transforming, and integrating of data in order to make it ready for analysis. The goal of data preprocessing is to improve the quality of the data and to make it more suitable for the specific data mining task.
rm(list = ls()) # Clear R Environment.
library(readr) # Package for importing csv files.
# Load data:
read_csv("C:\\Users\\nhainth\\Desktop\\Đối_soát\\rawdata\\export_data_id_TOPUP_MOBILE_1700107610.csv") -> topupData
read_csv("C:\\Users\\nhainth\\Desktop\\Đối_soát\\rawdata\\REPORTS_DETAILS_11_0_57_16_11_2023.csv") -> reportDataThe dplyr package in R is a powerful tool to do data munging and data manipulation, perhaps more so than many people would initially realize, making it extremely useful in data science.
# 3. dplyr::rename()
df5 %>% rename(thu_tu = TT,
code = `MÃ ĐƠN HÀNG`,
status = `TRẠNG THÁI`,
price1 = `NẠP THÀNH CÔNG (VNĐ)`,
price2 = `THANH TOÁN (VNĐ)`) -> df5
df5
df5 %>% select(c(1, 2, 5)) -> df_3cot
df_3cot %>% rename(a = thu_tu,
b = code,
c = price2)
names(df_3cot) <- c("a", "b", "c")
df_3cot
names(df_3cot) <- c("x1", "x2", "x3")# An example of using group_by() for calculating
# many statistics by group or or category:
reportData %>% select(7, 9) -> df_2cot # Only select some columns.
names(df_2cot) <- c("provider", "priceVND") # Rename for all columns.
# Some statistics by provider:
df_2cot %>%
group_by(provider) %>%
summarise(N = n(), Total = sum(priceVND), SD = sd(priceVND)) -> df_results
# Show results:
df_results# Create two data frames for illustration:
df1 <- data.frame(ten = c("dung", "an", "hue"),
age = c(21, 22, 24))
df2 <- data.frame(ten = c("trung", "hue", "dung"),
score = c(7, 9, 2))
full_join(df1, df2, by = c("ten")) # ket hop df1 va df2 thanh 1 bang
left_join(df1, df2, by = c("ten")) # Ket hop nhung uu tien df1
right_join(df1, df2, by = c("ten")) # Ket hop nhung uu tien df2
#========================================
# An application for reconciliation task
#========================================
rm(list = ls()) # Clear R Environment.
read_csv("C:\\Users\\nhainth\\Desktop\\Đối_soát\\rawdata\\export_data_id_TOPUP_MOBILE_1700107610.csv") -> topupData
read_csv("C:\\Users\\nhainth\\Desktop\\Đối_soát\\rawdata\\REPORTS_DETAILS_11_0_57_16_11_2023.csv") -> reportData
#---- Stage 1: Pre-processing for reportData -----
reportData %>% select(2, 7, 9, 11) -> reportDataPro
names(reportDataPro) <- c("code", "provider", "priceVND", "payment")
reportDataPro %>% mutate(codeNew = str_replace_all(code, "9pay_", "")) -> reportDataPro
reportDataPro
#---- Stage 2: Pre-processing for topupData -----
topupData %>% select(7, 9, 10, 14) -> topupDataPro
names(topupDataPro) <- c("code9pay", "nhamang", "amount", "sotienTT")
topupDataPro
#---- Stage 3: Join the two data sets -------
full_join(reportDataPro, topupDataPro, by = c("codeNew" = "code9pay")) -> dfJoined # ket họp 2 data ve 1 bảng theo du lieu
dfJoined
# Missing transactions:
dfJoined %>%
filter(is.na(code)) #loc ca quan sat thoa man 1 hoạc 1 loat cac dieu kien
dfJoined %>%
filter(priceVND >= 100000 & provider == "Viettel") #2 dieu kien cach nhau boi dau &
dfJoined %>%
filter(is.na(code)) #loc ra nhung dong bị khong trùng nhau (is.na (code): kiem tra nhưng dòng k có ở cot code)
dfJoined %>%
filter(!is.na(code)) # loc ra nhung dong bị khong trùng nhau (!is.na (code): kiem tra nhưng dòng khong bị ở cot code)
dfJoined %>%
mutate(providerNew = str_to_lower((provider))) -> dfJoinedFitted
dfJoinedFitted %>%
mutate(providerkhop = providerNew == nhamang) -> dfJoinedFitted
dfJoinedFitted %>%
group_by(providerkhop) %>%
summarise((TongsoGD = n()), totalprice = sum(priceVND)) #n() chính là đêm trong excel
dfJoinedFitted %>% filter(providerkhop == FALSE)
#=============================
#Du lieu 01/11-2023
#=============================
rm(list = ls())
read_csv("C:\\Users\\nhainth\\Desktop\\Đối_soát\\Topup\\Ngay011123\\export_data_id_TOPUP_MOBILE_1702291343.csv") -> topup0111
# install.packages("readxl")
library(readxl)
read_xls("C:\\Users\\nhainth\\Desktop\\Đối_soát\\Topup\\Ngay011123\\REPORTS_DETAILS_17_0_59_11_12_2023.xls", skip = 7) -> report0111
report0111 %>%
select(c(3, 5, 9)) -> report0111
report0111 %>%
mutate(Mamoi = str_replace_all(`MÃ REQUEST`, "9pay_", "")) -> report0111
names(report0111) <- c( "MaKQ", "LoaiSP", "Gia", "Mamoi")
topup0111 %>% select(c(2, 6, 7)) ->topup0111
names(topup0111) <- c("MaDH", "goicuoc", "Macode")
topup0111
full_join(report0111, topup0111, by = c("Mamoi" = "Macode")) ->Datamoi
Datamoi %>%
filter(is.na(Mamoi))