data <- read.csv("bank latih.csv", sep = ";")
head(data, 20)
any(is.na(data))
## [1] FALSE
Artinya tidak ada data yang missing
# baris
anyDuplicated(data)
## [1] 0
# kolom
duplicated_cols <- duplicated(t(data))
sum(duplicated_cols)
## [1] 0
# memeriksa apakah ada variabel yang beda nama tapi memiliki arti yang sama
cor_matrix <- cor(data[sapply(data, is.integer)])
library(pheatmap)
## Warning: package 'pheatmap' was built under R version 4.4.3
# Membuat heatmap dengan pheatmap
pheatmap(cor_matrix,
color = colorRampPalette(c("blue", "white", "red"))(50),
main = "Heatmap Korelasi Kolom Integer")
Artinya tidak ada duplikat data baik di baris maupun kolom. Selain itu,
berdasarkan hasil matriks korelasi di atas terlihat tidak ada korelasi
yang kuat antar variabel sehingga dapat dikatakan bahwa tidak ada
variabel yang redundan.
# Untuk mengetahui tipe data setiap variabel
str(data)
## 'data.frame': 4521 obs. of 17 variables:
## $ Age : int 30 33 35 30 59 35 36 39 41 43 ...
## $ job : chr "unemployed" "services" "management" "management" ...
## $ marital : chr "married" "married" "single" "married" ...
## $ education: chr "primary" "secondary" "tertiary" "tertier" ...
## $ default : chr "no" "no" "no" "no" ...
## $ balance : int 1787 4789 1350 1476 0 747 307 147 221 -88 ...
## $ housing : chr "no" "yes" "yes" "yes" ...
## $ loan : chr "no" "yes" "no" "yes" ...
## $ contact : chr "cellular" "cellular" "cellular" "unknown" ...
## $ day : int 19 11 16 3 5 23 14 6 14 17 ...
## $ month : chr "10" "may" "apr" "jun" ...
## $ duration : int 79 220 185 199 226 141 341 151 57 313 ...
## $ campaign : int 1 1 1 4 1 2 1 2 2 1 ...
## $ pdays : int -1 339 330 -1 -1 176 330 -1 -1 147 ...
## $ previous : int 0 4 1 0 0 3 2 0 0 2 ...
## $ poutcome : chr "unknown" "failure" "failure" "unknown" ...
## $ y : chr "no" "no" "no" "no" ...
cat("\n")
# Untuk mengetahui ada apa saja macam isian dan untuk melihat apakah ada kata yang typo atau bersinonim
lapply(data[, sapply(data, is.character)], unique)
## $job
## [1] "unemployed" "services" "management" "blue-collar"
## [5] "self-employed" "technician" "entrepreneur" "admin."
## [9] "student" "housemaid" "retired" "unknown"
##
## $marital
## [1] "married" "single" "menikah" "divorced"
##
## $education
## [1] "primary" "secondary" "tertiary" "tertier" "sekunder" "unknown"
##
## $default
## [1] "no" "yes"
##
## $housing
## [1] "no" "yes" "tidak"
##
## $loan
## [1] "no" "yes" "tidak"
##
## $contact
## [1] "cellular" "unknown" "seluler" "telephone"
##
## $month
## [1] "10" "may" "apr" "jun" "feb" "aug" "jan" "7" "nov" "jul" "oct" "sep"
## [13] "mar" "dec"
##
## $poutcome
## [1] "unknown" "failure" "other" "success"
##
## $y
## [1] "no" "iya" "yes"
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Mengganti "tidak" dengan "no" di variabel housing dan loan
data <- data %>%
mutate(across(c(housing, loan), ~ gsub("tidak", "no", .)))
# Mengganti "menikah" denagn "married" di variabel marital
data <- data %>%
mutate(across(marital, ~ gsub("menikah", "married", .)))
# Mengganti "tertier" denagn "tertiary" di variabel education
data <- data %>%
mutate(education = case_when(
education == "tertier" ~ "tertiary",
education == "sekunder" ~ "secondary",
TRUE ~ education
))
# Mengganti "seluler" dengan "cellular" di variabel contact
data <- data %>%
mutate(across(contact, ~ gsub("seluler", "cellular", .)))
# Megganti "iya" dengan "yes" di variabel y
data <- data %>%
mutate(across(y, ~ gsub("iya", "yes", .)))
nama_bulan <- c("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec")
data <- data %>%
mutate(month = ifelse(month %in% as.character(1:12),
nama_bulan[as.numeric(month)],
month))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `month = ifelse(...)`.
## Caused by warning in `ifelse()`:
## ! NAs introduced by coercion
# dicek lagi
lapply(data[, sapply(data, is.character)], unique)
## $job
## [1] "unemployed" "services" "management" "blue-collar"
## [5] "self-employed" "technician" "entrepreneur" "admin."
## [9] "student" "housemaid" "retired" "unknown"
##
## $marital
## [1] "married" "single" "divorced"
##
## $education
## [1] "primary" "secondary" "tertiary" "unknown"
##
## $default
## [1] "no" "yes"
##
## $housing
## [1] "no" "yes"
##
## $loan
## [1] "no" "yes"
##
## $contact
## [1] "cellular" "unknown" "telephone"
##
## $month
## [1] "oct" "may" "apr" "jun" "feb" "aug" "jan" "jul" "nov" "sep" "mar" "dec"
##
## $poutcome
## [1] "unknown" "failure" "other" "success"
##
## $y
## [1] "no" "yes"
head(data, 20)
#Graphical Boxplot Test
par(mfrow = c(2, 2))
# Pilih kolom numerik saja
num_cols <- sapply(data, is.numeric)
# Loop hanya untuk kolom numerik
for(i in which(num_cols)) {
boxplot(data[[i]], main = names(data)[i])
}
library(writexl)
## Warning: package 'writexl' was built under R version 4.4.3
write_xlsx(data, "hasil_prepo_bank.xlsx")