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.
The 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")) # Keeps all observations in df1 and df2.
left_join(df1, df2, by = c("ten")) # Keeps all observations in df1.
right_join(df1, df2, by = c("ten")) # Keeps all observations in df2.
#========================================
# An application for reconciliation task
#========================================
rm(list = ls()) # Clear R Environment.
read_csv("E:\\doisoat\\rawdata\\export_data_id_TOPUP_MOBILE_1700107610.csv") -> topupData
read_csv("E:\\doisoat\\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
#---- Stage 2: Pre-processing for topupData -----
topupData %>% select(7, 9, 10, 14) -> topupDataPro
names(topupDataPro) <- c("code9pay", "nhamang", "amount", "sotienTT")
#---- Stage 3: Join the two data sets -------
full_join(reportDataPro, topupDataPro, by = c("codeNew" = "code9pay")) -> dfJoined
# Missing transactions:
dfJoined %>%
filter(is.na(code))dfJoined %>% filter(priceVND >= 100000 & provider == "viettel")
dfJoined %>% filter(!is.na(code))-> dfJoinedFitted
dfJoinedFitted %>%
mutate(providerNew = str_to_lower(provider)) -> dfJoinedFitted
dfJoinedFitted %>%
mutate(khop_provider = providerNew == nhamang) -> dfJoinedFitted
dfJoinedFitted %>%
group_by(khop_provider) %>%
summarise(tongsoGD = n(), totalPrice = sum (priceVND))
dfJoinedFitted %>% filter(khop_provider == FALSE)
#=====================================
#New Projecr for 01 - 11 - 2023
rm(list = ls()) # Clear R Environment.
library(readr)
read_csv("E:\\Topup\\Topup\\Ngay011123\\export_data_id_TOPUP_MOBILE_1702291343.csv") -> topup0111
# install.packages("readxl")
library(readxl)
#skip =7 : bo di 7 giao dich
read_xls("E:\\Topup\\Topup\\Ngay011123\\REPORTS_DETAILS_17_0_59_11_12_2023.xls", skip = 7) -> report0111
#chon cot va doi ten cot topup
library(dplyr)
library(stringr)
topup0111 %>% select(5, 7, 9, 10, 14) -> topup0111
names(topup0111) <- c("type", "code9pay", "nhamang", "amount", "sotienTT")
#chon cot va doi ten cot report
report0111 %>% select(2, 7, 9, 11) -> report0111
names(report0111) <- c("code", "provider", "priceVND", "payment")
report0111 %>% mutate(codeNew = str_replace_all(code, "9pay_", "")) -> report0111
#so sanh ma code 2 du lieu
full_join(report0111, topup0111, by = c("codeNew" = "code9pay")) -> dfJoined
#loc cac giao dich loi
dfJoined %>%
filter(is.na(code)) -> dfJoinedNa
#loc cac giao dich khong loi
dfJoined %>% filter(!is.na(code))-> dfJoinedFitted
#kiem tra cac giao dich khong loi
dfJoinedFitted %>%
mutate(providerNew = str_to_lower(provider)) -> dfJoinedFitted
# kiem tra khop nha mang type topupmobile
dfJoinedFitted %>%
filter(type == "TOPUP_MOBILE") %>% mutate(khop_provider = providerNew == nhamang) -> dfJoinedMobile
# kiem tra khop so tien type topupmobile
dfJoinedMobile %>%
mutate(khop_price = priceVND == amount) -> dfJoinedMobile
#thong ke theo nha cung cap
dfJoinedMobile %>%
group_by(providerNew) %>%
summarise(N = n(), Total = sum(priceVND)) -> df_resultsMobile
# kiem tra khop nha mang type topupdata
dfJoinedFitted %>%
filter(type == "TOPUP_DATA") %>% mutate(nhamang1 = str_replace_all(nhamang, "vinaphone", "vnpdata")) %>% mutate(nhamang1 = str_replace_all(nhamang1, "viettel", "vttdata"))%>% mutate(nhamang1 = str_replace_all(nhamang1, "mobifone", "thẻ data mobifone"))-> dfJoinedData
dfJoinedData %>%
mutate(khop_provider2 = providerNew == nhamang1) -> dfJoinedData
# kiem tra khop so tien type topupdata
dfJoinedData %>%
mutate(khop_price = priceVND == amount) -> dfJoinedData
#thong ke theo nha cung cap
dfJoinedData %>%
group_by(providerNew) %>%
summarise(N = n(), Total = sum(priceVND)) -> df_resultsData