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("D:\\doisoat\\rawdata\\export_data_id_TOPUP_MOBILE_1700107610.csv") -> topupData
read_csv("D:\\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 Project for 01 - 11-2023
#=====================
rm(list = ls())
read_csv("C:\\Users\\VanPhucComputer\\Downloads\\Telegram Desktop\\Topup\\Topup\\Ngay011123\\export_data_id_TOPUP_MOBILE_1702291343.csv") -> topup0111
library(readxl)
install.packages("readxl")
read_xls("C:\\Users\\VanPhucComputer\\Downloads\\Telegram Desktop\\Topup\\Topup\\Ngay011123\\REPORTS_DETAILS_17_0_59_11_12_2023.xls", skip = 7) -> report0111
report0111 %>% rename(code = `MÃ ĐƠN HÀNG`,
codeRequest = `MÃ REQUEST`,
create = `NGƯỜI TẠO`,
provider = `LOẠI SẢN PHẨM`) -> report0111
report0111 %>% mutate(code = str_replace_all(code, "9pay_", "")) -> report0111
topup0111 %>% rename (code = `Mã 9Pay Code`) -> topup0111
full_join(report0111, topup0111, by = c("code" = "code")) -> fileJoined