Data Science Progamming
April 20, 2025
1 Soal 1
1. (CPL 11 dan CPL 13, 20%) Operasi dan Tipe Data Dasar
Buat program sederhana dalam R dan Python yang melakukan hal berikut:
Menerima dua bilangan dari pengguna
Menghitung dan menampilkan hasil:
- Penjumlahan
- Perkalian
- Pembagian
- Bilangan pertama pangkat bilangan kedua
- Menampilkan tipe data masing-masing hasil operasi
bil1 <- 2
bil2 <- 6
jumlah <- bil1 + bil2
kali <- bil1 * bil2
bagi <- bil1 / bil2
pangkat <- bil1 ^ bil2
cat("\n")
## Penjumlahan: 8
## Perkalian: 12
## Pembagian: 0.3333333
## Pangkat: 64
## Hasil penjumlahan = 8 dengan tipe data numeric
## Hasil perkalian = 12 dengan tipe data numeric
## Hasil pembagian = 0.3333333 dengan tipe data numeric
## Hasil pangkat = 64 dengan tipe data numeric
2 Soal 2
- (CPL 1 dan CPL 2, 20%) Struktur Kendali (Control Flow)
Tulislah program dalam R dan Python yang:
Menerima input nilai ujian dari pengguna (0–100)
Menampilkan keterangan berdasarkan ketentuan berikut:
- Nilai ≥ 85: “Sangat Baik”
- Nilai 70–84: “Baik”
- Nilai 60–69: “Cukup”
- Nilai < 60: “Perlu Perbaikan”
nilai <- 74
if (nilai >= 85) {
cat("Sangat Baik\n")
} else if (nilai >= 70 && nilai <= 84) {
cat("Baik\n")
} else if (nilai >= 60 && nilai <= 69) {
cat("Cukup\n")
} else {
cat("Perlu Perbaikan\n")
}
## Baik
3 Soal 3
- (CPL_KU_01 dan CPL_KU_01, 20%) Fungsi dan Perulangan
Buatlah fungsi dalam R dan Python bernama kelipatan_genap(n) yang:
Menerima input integer n
Menggunakan loop untuk mencetak semua bilangan genap kelipatan 4 dari 1 hingga n
Contoh output jika n = 20: 4, 8, 12, 16, 20
kelipatan_genap <- function(n) {
hasil <- c()
for (i in 1:n) {
if (i %% 2 == 0 && i %% 4 == 0) {
hasil <- c(hasil, i)
}
}
cat(paste(hasil, collapse = ", "), "\n")
}
angka <- 40
kelipatan_genap(angka)
## 4, 8, 12, 16, 20, 24, 28, 32, 36, 40
4 Gabung dan Tampilkan Data
library(readr)
library(dplyr)
januari <- read_csv("data/penjualan-januari.csv")
februari <- read_csv("data/penjualan-februari.csv")
maret <- read_csv("data/penjualan-maret.csv")
df <- bind_rows(januari, februari, maret)
cat("Penggabungan Dataset:\n")
## Penggabungan Dataset:
## # A tibble: 150 × 9
## OrderID Tanggal Produk Kategori Harga Jumlah Total Pembeli Kota
## <dbl> <chr> <chr> <chr> <chr> <chr> <lgl> <chr> <chr>
## 1 1001 01-01-2024 Laptop A <NA> 15000… empat NA Budi@ Band…
## 2 1002 02-01-2024 Tas Branded Fashion 15000… 2 NA Budi@ Sura…
## 3 1003 03-01-2024 Tas Branded Aksesoris 15000… 3 NA _anony… Band…
## 4 1004 04-01-2024 Kemeja <NA> 15000… dua NA Andi123 Band…
## 5 1005 05-01-2024 - Fashion Rp750… 3 NA _anony… Jaka…
## 6 1006 06-01-2024 Smartphone X Fashion 15000… empat NA Sinta99 Band…
## 7 1007 07-01-2024 Sepatu 'Nike' Elektronik 1.200… 3 NA _anony… Band…
## 8 1008 08-01-2024 - Elektronik 250000 dua NA _anony… Jaka…
## 9 1009 09-01-2024 Smartphone X Elektronik 15000… empat NA _anony… -
## 10 1010 10-01-2024 Sepatu 'Nike' Aksesoris Rp750… 2 NA Sinta99 Sura…
## # ℹ 140 more rows
##
## Jumlah baris: 150
## Jumlah kolom: 9
5 Data Cleaning
library(dplyr)
library(readr)
library(lubridate)
library(stringr)
# === 1. Baca dan Gabungkan Data ===
januari <- read_csv("data/penjualan-januari.csv", locale = locale(encoding = "ISO-8859-1"))
februari <- read_csv("data/penjualan-februari.csv", locale = locale(encoding = "ISO-8859-1"))
maret <- read_csv("data/penjualan-maret.csv", locale = locale(encoding = "ISO-8859-1"))
df <- bind_rows(januari, februari, maret)
# === 2. Format Tanggal ===
df$Tanggal <- as.Date(df$Tanggal, format = "%Y-%m-%d")
# === 3. Bersihkan dan Konversi Kolom Harga dan Jumlah ===
df$Harga <- as.numeric(str_replace_all(as.character(df$Harga), "[^0-9.]", ""))
## Warning: NAs introduced by coercion
df$Jumlah <- as.numeric(str_replace_all(as.character(df$Jumlah), "[^0-9.]", ""))
# === 4. Hitung Kolom Total ===
df$Total <- df$Harga * df$Jumlah
# === 5. Ganti Nilai Tidak Valid ===
nilai_tidak_valid <- c("-", "dua", "Rp", "_anonymous_")
df <- df %>%
mutate(across(everything(), ~replace(.x, .x %in% nilai_tidak_valid, NA)))
# === 6. Hapus Baris Tanpa Nama Produk ===
df <- df %>%
filter(!is.na(Produk), Produk != "", Produk != "-")
# === 7. Hapus Baris dengan NA di Kolom Wajib ===
kolom_wajib <- c("Tanggal", "Harga", "Jumlah", "Total")
df <- df %>%
filter(if_all(all_of(kolom_wajib), ~!is.na(.)))
# === 8. Tampilkan Informasi dan Data Awal ===
cat(strrep("=", 40), "\n")
## ========================================
## INFORMASI DATA
## ========================================
## tibble [58 × 9] (S3: tbl_df/tbl/data.frame)
## $ OrderID : num [1:58] 1002 1003 1010 1013 1016 ...
## $ Tanggal : Date[1:58], format: "0002-01-20" "0003-01-20" ...
## $ Produk : chr [1:58] "Tas Branded" "Tas Branded" "Sepatu 'Nike'" "Laptop A" ...
## $ Kategori: chr [1:58] "Fashion" "Aksesoris" "Aksesoris" "Aksesoris" ...
## $ Harga : num [1:58] 1.5e+07 1.5e+07 7.5e+02 7.5e+02 1.5e+07 1.5e+07 7.5e+02 7.5e+02 1.5e+07 1.5e+07 ...
## $ Jumlah : num [1:58] 2 3 2 2 2 1 2 3 2 3 ...
## $ Total : num [1:58] 30000000 45000000 1500 1500 30000000 15000000 1500 2250 30000000 45000000 ...
## $ Pembeli : chr [1:58] "Budi@" NA "Sinta99" "Sinta99" ...
## $ Kota : chr [1:58] "Surabaya" "Bandung" "Surabaya" "Surabaya" ...
## NULL
## ========================================
## DATA YANG DIHASILKAN
## ========================================
## # A tibble: 6 × 9
## OrderID Tanggal Produk Kategori Harga Jumlah Total Pembeli Kota
## <dbl> <date> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr>
## 1 1002 0002-01-20 Tas Branded Fashion 1.5e7 2 3 e7 Budi@ Sura…
## 2 1003 0003-01-20 Tas Branded Aksesoris 1.5e7 3 4.50e7 <NA> Band…
## 3 1010 0010-01-20 Sepatu 'Nike' Aksesoris 7.5e2 2 1.5 e3 Sinta99 Sura…
## 4 1013 0013-01-20 Laptop A Aksesoris 7.5e2 2 1.5 e3 Sinta99 Sura…
## 5 1016 0016-01-20 Kemeja Aksesoris 1.5e7 2 3 e7 <NA> Sura…
## 6 1021 0021-01-20 Laptop A Elektronik 1.5e7 1 1.5 e7 <NA> Jaka…
6 Data Transformation
library(dplyr)
library(readr)
library(lubridate)
library(stringr)
# =======================
# Membaca dan Menggabungkan Data
# =======================
januari <- read_csv("data/penjualan-januari.csv", locale = locale(encoding = "ISO-8859-1"))
februari <- read_csv("data/penjualan-februari.csv", locale = locale(encoding = "ISO-8859-1"))
maret <- read_csv("data/penjualan-maret.csv", locale = locale(encoding = "ISO-8859-1"))
df <- bind_rows(januari, februari, maret)
# =======================
# Pembersihan dan Transformasi Data
# =======================
# Ubah kolom "Tanggal" ke Date, gagal parsing jadi NA
df <- df %>%
mutate(
Tanggal = as.Date(Tanggal),
Bulan = format(Tanggal, "%Y-%m")
)
# Bersihkan nilai non-numerik dan ubah ke numerik
df <- df %>%
mutate(
Harga = as.numeric(str_replace_all(as.character(Harga), "[^0-9.]", "")),
Jumlah = as.numeric(str_replace_all(as.character(Jumlah), "[^0-9.]", "")),
Total = Harga * Jumlah
)
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Harga = as.numeric(str_replace_all(as.character(Harga),
## "[^0-9.]", ""))`.
## Caused by warning:
## ! NAs introduced by coercion
# =======================
# Analisis Data
# =======================
# Total penjualan per kategori
total_per_kategori <- df %>%
group_by(Kategori) %>%
summarise(Total = sum(Total, na.rm = TRUE), .groups = "drop")
# Jumlah transaksi per kota
transaksi_per_kota <- df %>%
count(Kota, name = "Jumlah_Transaksi") %>%
arrange(desc(Jumlah_Transaksi))
# Total penjualan per bulan
penjualan_per_bulan <- df %>%
group_by(Bulan) %>%
summarise(Total = sum(Total, na.rm = TRUE), .groups = "drop")
# =======================
# Output Hasil Analisis
# =======================
cat(strrep("=", 40), "\n")
## ========================================
## TOTAL PENJUALAN PER KATEGORI
## ========================================
## # A tibble: 4 × 2
## Kategori Total
## <chr> <dbl>
## 1 Aksesoris 332264250
## 2 Elektronik 212756750
## 3 Fashion 225259750
## 4 <NA> 168762000
##
## ========================================
## JUMLAH TRANSAKSI PER KOTA
## ========================================
## # A tibble: 4 × 2
## Kota Jumlah_Transaksi
## <chr> <int>
## 1 - 38
## 2 Jakarta 38
## 3 Bandung 37
## 4 Surabaya 37
##
## ========================================
## TOTAL PENJUALAN PER BULAN
## ========================================
## # A tibble: 110 × 2
## Bulan Total
## <chr> <dbl>
## 1 0001-01 0
## 2 0001-02 250000
## 3 0001-03 0
## 4 0001-04 0
## 5 0002-01 30000000
## 6 0002-02 1500
## 7 0002-03 45001500
## 8 0002-04 0
## 9 0003-01 45000000
## 10 0003-02 15000000
## # ℹ 100 more rows
# =========================
# 📦 Load Library
# =========================
library(dplyr)
library(readr)
library(lubridate)
library(stringr)
library(ggplot2)
library(plotly)
# =========================
# 📁 Baca dan Gabungkan Data
# =========================
januari <- read_csv("data/penjualan-januari.csv", locale = locale(encoding = "ISO-8859-1"))
februari <- read_csv("data/penjualan-februari.csv", locale = locale(encoding = "ISO-8859-1"))
maret <- read_csv("data/penjualan-maret.csv", locale = locale(encoding = "ISO-8859-1"))
df <- bind_rows(januari, februari, maret)
# =========================
# 🧹 Pembersihan dan Transformasi Data
# =========================
df <- df %>%
mutate(
Tanggal = as.Date(Tanggal, format = "%Y-%m-%d"),
Bulan = format(Tanggal, "%Y-%m"),
Harga = as.numeric(str_replace_all(as.character(Harga), "[^0-9.]", "")),
Jumlah = as.numeric(str_replace_all(as.character(Jumlah), "[^0-9.]", "")),
Total = Harga * Jumlah
)
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Harga = as.numeric(str_replace_all(as.character(Harga),
## "[^0-9.]", ""))`.
## Caused by warning:
## ! NAs introduced by coercion
# =========================
# 📊 Analisis Data
# =========================
# a. Total penjualan per kategori
total_per_kategori <- df %>%
group_by(Kategori) %>%
summarise(Total = sum(Total, na.rm = TRUE), .groups = "drop")
# b. Jumlah transaksi per kota
transaksi_per_kota <- df %>%
count(Kota, name = "Jumlah_Transaksi") %>%
arrange(desc(Jumlah_Transaksi))
# c. Total penjualan per bulan
penjualan_per_bulan <- df %>%
group_by(Bulan) %>%
summarise(Total = sum(Total, na.rm = TRUE), .groups = "drop")
# =========================
# 🎨 Visualisasi Data
# =========================
warna_biru_gradasi <- c('#1f77b4', '#2a80c9', '#3d8edb', '#539df0', '#6bb0ff', '#89c4ff', '#aed9ff')
# 1. Grafik Batang - Total Penjualan per Kategori
bar_plot <- ggplot(total_per_kategori, aes(x = Kategori, y = Total, fill = Kategori)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = warna_biru_gradasi) +
labs(title = "Total Penjualan per Kategori - Grafik Batang", x = "Kategori", y = "Total Penjualan") +
theme_minimal()
ggplotly(bar_plot)
# 2. Grafik Donat - Jumlah Transaksi per Kota
donut_plot <- plot_ly(
transaksi_per_kota,
labels = ~Kota,
values = ~Jumlah_Transaksi,
type = 'pie',
hole = 0.4,
textinfo = 'label+percent',
marker = list(colors = warna_biru_gradasi)
) %>%
layout(title = 'Jumlah Transaksi per Kota - Grafik Donat')
donut_plot
# 3. Grafik Garis - Total Penjualan per Bulan
line_plot <- ggplot(penjualan_per_bulan, aes(x = Bulan, y = Total, group = 1)) +
geom_line(color = "#1f77b4") +
geom_point(color = "#1f77b4") +
labs(title = "Total Penjualan per Bulan - Grafik Garis", x = "Bulan", y = "Total Penjualan") +
theme_minimal()
ggplotly(line_plot)