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())
install.packages("readxl")
library(readxl)
library(readr)
library(dplyr)
#========== Stage 1: Load and Pre-processing =========
read_csv("D:\\Ngay011123\\export_data_id_TOPUP_MOBILE_1702291343.csv") -> data9pay
read_xls("D:\\Ngay011123\\REPORTS_DETAILS_17_0_59_11_12_2023.xls", skip = 7) -> dataPartner
# Rename for all columns:
dataPartner %>% select(-1, -2) -> dataPartner
names(dataPartner) <- c("ma", "nguoitao", "loaiSP", "thoigian", "nhacungcap",
"sodt", "menhgia", "napthanhcong", "thanhtoan", "trangthai")
data9pay %>% select(-19, -18, -17) -> data9pay
names(data9pay) <- c("mathanhtoan", "madonhang", "magiaodich", "mavi", "doitac",
"type", "goicuoc", "ma9pay", "madoitac", "nhamang", "amount",
"voucher", "phithu", "phitra", "sotienTT", "statusTrans", "statusOrder")
library(stringr)
dataPartner %>%
mutate(maNew = str_replace_all(ma, "9pay_", ""), nhacungcap = str_to_lower(nhacungcap)) -> dataPartner
#======== Stage 2: Join the two data sets ==========
full_join(data9pay,
dataPartner,
by = c("ma9pay" = "maNew")) -> dfCombined
dfCombined %>% select(ma9pay, ma, everything()) -> dfCombined
#======= Stage 3: Check reconciliation =======
dfCombined %>% mutate(is_ok_code = case_when(is.na(ma9pay) ~ "no",
TRUE ~ "yes")) -> dfCombined
dfCombined %>%
group_by(is_ok_code) %>%
summarise(N = n())rm(list = ls())
install.packages("readxl")
library(readxl)
library(readr)
library(dplyr)
read_csv("C:\\Users\\funtap\\Desktop\\Topup\\Thang112023\\export_data_id_TOPUP_MOBILE_1702290936.csv") -> cua9payThang11
read_xls("C:\\Users\\funtap\\Desktop\\Topup\\Thang112023\\REPORTS_DETAILS_17_0_26_11_12_2023.xls", skip = 7) -> cuaEpayThang11library(stringi) # xu ly text
names(cua9payThang11) -> oldname1
#doi chu thanh chu khong dau
stri_trans_general(oldname1, "Latin-ASCII") #cach1
library(stringr)
oldname1 %>%
stri_trans_general("Latin-ASCII") %>%
str_replace_all(" ", "") -> newnames1 #cach2
names(cua9payThang11) <- newnames1
names(cuaEpayThang11) -> oldname2
oldname2 %>%
stri_trans_general("Latin-ASCII") %>%
str_replace_all(" ", "") %>%
str_replace_all("\\(", "") %>%
str_replace_all("\\)", "") -> newnames2
names(cuaEpayThang11) <- newnames2
glimpse(cua9payThang11)
#xu ly du lieu thoi gian
install.packages("lubridate")
library(lubridate)
install.packages("ggplot2")
cua9payThang11 %>%
select(Mathanhtoan, Updated) %>%
mutate(dateYMD = date(Updated)) %>%
mutate(thang = month(dateYMD, label = FALSE)) %>%
mutate(ngay = day(dateYMD)) -> cua9paymini
ten <- c("a", "b", "c", "a")
duplicated(ten)
ten %>% duplicated() %>% sum()
cua9paymini %>% pull(Mathanhtoan) %>% duplicated() %>% sum()
cua9payThang11 %>% pull(Type) %>% duplicated() %>% sum()
cua9payThang11 %>%
group_by(Type) %>%
summarise(feq = n())
cua9payThang11 %>%
filter(duplicated(Type))
library(ggplot2)
iris %>%
ggplot(aes(x= Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
facet_wrap(~ Species)