Introduction to Data Science
TUGAS 2
Kontak | : \(\downarrow\) |
: dhelaagatha@gmail.com | |
: https://www.instagram.com/dhelaasf/ | |
RPubs | : https://rpubs.com/dhelaasafiani/ |
NIM | : 20214920009 |
Prodi | : Statistika Bisnis 2021 |
Nama | : Dhela Asafiani Agatha |
Project Kelompok oleh :
1. Dhela Asafiani Agatha
2. Sausan Ramadhani
3. Valensius Jimy
Soal
“This is a transnational data set which contains all the transactions occurring between 01/12/2010 and 09/12/2011 for a UK-based and registered non-store online retail. The company mainly sells unique all-occasion gifts. Many customers of the company are wholesalers.”
Pengenalan Project
Pada kesempatan project kelompok kali ini, kami akan melakukan proses import data, cleaning and wragling data dan visualisasi data untuk mencari informasi mengenai sebuah penjualan perusahaan online retail. Berikut adalah perpustakaan yang kami gunakan untuk project kali ini.
library(tidyverse)
library(lubridate)
library(scales)
library(zoo)
library(plotly)
library(ggplot2)
library(glue)
Berikut adalah Link penjelasan pengerjaan kelompok kami https://drive.google.com/file/d/1nh-KZdwI7UK48iPcmyWgmLJ2y4GByvQZ/view
Import Data
getwd()
## [1] "C:/Users/hp/Documents/SEMESTER 2/PENGANTAR DATA SCIENCE"
library(tidyverse)
<- read.csv("C:/Users/hp/Documents/SEMESTER 2/PENGANTAR DATA SCIENCE/data_ecommerce.csv")
data_ecommerce data_ecommerce
# Melihat banyak frekuensi
%>%
data_ecommerce count(InvoiceNo)
%>%
data_ecommerce count(StockCode)
%>%
data_ecommerce count(Description)
%>%
data_ecommerce count(Quantity)
%>%
data_ecommerce count(InvoiceDate)
%>%
data_ecommerce count(UnitPrice)
%>%
data_ecommerce count(CustomerID)
%>%
data_ecommerce count(Country)
Memisahkan date dan time menjadi 2 kolom.
<- separate(data_ecommerce,
data_ecommerce1 col = InvoiceDate,
into = c("Invoice_Date", "Time"),
sep = " "
)
data_ecommerce1
Data Cleaning
Transaksi Batal
Berdasarkan informasi yang kami dapat dapat diartikan bahwa InvoiceNo
yang diawali dengan huruf C berarti transaksi tersebut bersifat cancel atau batal, maka kita harus cek apakah pada data ini terdapat transaksi yang gagal:
length(which(substr(str_to_upper(data_ecommerce1$InvoiceNo),1,1) == "C")) #Number of cancellations
## [1] 9288
# terdapat 9288 transaksi yang bersifat cancel, sehingga kita dapat remove untuk transaksi tersebut dengan cara:
<- data_ecommerce1 %>%
data_ecommerce2 filter(!grepl("C", data_ecommerce1$InvoiceNo))
data_ecommerce2
Transaksi Tidak Valid
Berdasarkan Invoice
Transaksi yang valid harus memiliki InvoiceNo
yang terdiri dari 6 digit angka, maka kita harus cek pada data ini.
<- data_ecommerce2 %>%
data_ecommerce3 filter(!nchar(InvoiceNo)>6)
data_ecommerce3
Berdasarkan Kuantitas
Ketika ada kuantitas yang bernilai nol, maka sudah dipastikan tidak valid dengan alasan tidak mungkin transaksi dapat berjalan tanpa adanya barang yang keluar atau terjual.
%>%
data_ecommerce3 filter(Quantity<=0)
<- data_ecommerce3 %>%
data_ecommerce4 filter(Quantity>0)
data_ecommerce4
Berdasarkan Harga
Pada kasus ini tidak dijelaskan apakah ada promo atau hal lainnya yang membuat harga penjualan menjadi nol, sehingga kita dapat asumsikan bahwa ketika transaksi yang memiliki nilai nol adalah gagal dan dapat kita remove.
%>%
data_ecommerce4 filter(UnitPrice<=0) %>%
nrow()
## [1] 1179
<- data_ecommerce4 %>%
data_ecommerce5 filter(UnitPrice>0)
data_ecommerce5
data_ecommerce5
Berdasarkan Stok Kode
Berdasarkan informasi yang kami dapat juga ternyata Stock Code
yang valid terdiri dari 5 digit angka. Mari kita lakukan pengecekan untuk hal ini.
data.frame(
data = length(data_ecommerce5$StockCode),
data_unique=length(unique(data_ecommerce5$StockCode))
)
<- data_ecommerce5 %>%
data_ecommerce5 mutate(StockCode = toupper(StockCode))
<- data_ecommerce5 %>%
data_ecommerce5 mutate(data_stock = nchar(StockCode))
%>%
data_ecommerce5 group_by(data_stock)%>%
summarise(row = n())
Cek stock_kode berdigit 1 sampai 4
%>% filter(data_stock %in% c(1,2,3,4)) %>%
data_ecommerce5 select(StockCode,Description) %>%
distinct() %>% arrange(StockCode)
<- data_ecommerce5 %>%
data_ecommerce6 filter(!data_stock %in% c(1,2,3,4))
data_ecommerce6
Cek stock_kode berdigit lebih dari 6
%>% filter(nchar(StockCode)>6) %>%
data_ecommerce6 select(StockCode,Description) %>%
distinct() %>% arrange(StockCode)
<- data_ecommerce6 %>%
data_ecommerce7 filter(!nchar(StockCode)>6)
data_ecommerce7
Missing Values
Dalam mengolah sebuah data dan ingin memberikan sebuah informasi dari data yang kita olah, maka kita harus mengatasi yang namanya Missing Values yang kerapkali ditemukan ketika mengolah data yang banyak. Kita cek pada data yang saat ini sedang diolah:
colSums(is.na(data_ecommerce7))
## InvoiceNo StockCode Description Quantity Invoice_Date Time
## 0 0 0 0 0 0
## UnitPrice CustomerID Country data_stock
## 0 131296 0 0
Mengubah format Date
$month <- sapply(data_ecommerce7$Invoice_Date, FUN = function(x) {strsplit(x, split = '[/]')[[1]][1]})
data_ecommerce7$year <- sapply(data_ecommerce7$Invoice_Date, FUN = function(x) {strsplit(x, split = '[/]')[[1]][3]})
data_ecommerce7$hourOfDay <- sapply(data_ecommerce7$Time, FUN = function(x) {strsplit(x, split = '[:]')[[1]][1]})
data_ecommerce7$Invoice_Date <- as.Date(data_ecommerce7$Invoice_Date, "%m/%d/%Y")
data_ecommerce7library(lubridate)
$dayOfWeek <- wday(data_ecommerce7$Invoice_Date, label=TRUE) data_ecommerce7
Visualisasi Data
kita telah mendapat data yang bersih, sehingga dapat kita olah dalam bentuk visual untuk memberikan informasi yang baik dan jelas kepada orang lain.
Most 10 Popular Product by Frequency Order
library(plotly)
<- data_ecommerce7 %>% group_by(StockCode,Description) %>%
plot_most_frequency summarise(frequency = n()) %>%
ungroup() %>%
arrange(desc(frequency)) %>%
head(10) %>%
mutate(description = as.factor(Description),
description = reorder(Description,frequency)) %>%
ggplot(aes(x=description, y=frequency))+
geom_bar(stat="identity", aes(fill=description, text=frequency), show.legend = FALSE)+
labs(title="Most 10 Popular Product by Frequency Order",
x=NULL)+
coord_flip()
## `summarise()` has grouped output by 'StockCode'. You can override using the `.groups` argument.
## Warning: Ignoring unknown aesthetics: text
ggplotly(plot_most_frequency, tooltip="text") %>%
layout(showlegend=FALSE) %>%
config(displayModeBar = F, scrollzoom = F)
## Warning: 'config' objects don't have these attributes: 'scrollzoom'
## Valid attributes include:
## 'autosizable', 'displaylogo', 'displayModeBar', 'doubleClick', 'doubleClickDelay', 'editable', 'edits', 'fillFrame', 'frameMargins', 'globalTransforms', 'linkText', 'locale', 'locales', 'logging', 'mapboxAccessToken', 'modeBarButtons', 'modeBarButtonsToAdd', 'modeBarButtonsToRemove', 'notifyOnLogging', 'plotGlPixelRatio', 'plotlyServerURL', 'queueLength', 'responsive', 'scrollZoom', 'sendData', 'setBackground', 'showAxisDragHandles', 'showAxisRangeEntryBoxes', 'showEditInChartStudio', 'showLink', 'showSendToCloud', 'showSources', 'showTips', 'staticPlot', 'toImageButtonOptions', 'topojsonURL', 'watermark'
Note
: Dapat disimpulkan bahwa 10 produk teratas/terpopuler berdasarkan frekuensi pesanannya. Dimana produk yang paling populer/terbanyak di pesan adalah produk WHITE HANGING HEART T-LIGHT HOLDER.
Most 10 Popular Product by Total Customer
<- data_ecommerce7 %>%
plot_most_customer select(CustomerID, StockCode,Description) %>%
distinct() %>%
group_by(StockCode,Description) %>%
summarise(total_customer = n()) %>%
ungroup() %>%
arrange(desc(total_customer)) %>%
head(10) %>%
mutate(description = as.factor(Description),
description = reorder(Description,total_customer)) %>%
ggplot(aes(x=description, y=total_customer))+
geom_bar(stat="identity", aes(fill=description, text=total_customer), show.legend = FALSE)+
labs(title="Most 10 Popular Product by Total Customer",
x=NULL)+
coord_flip()
## `summarise()` has grouped output by 'StockCode'. You can override using the `.groups` argument.
## Warning: Ignoring unknown aesthetics: text
ggplotly(plot_most_customer, tooltip = "text") %>%
layout(showlegend=FALSE) %>%
config(displayModeBar = F, scrollzoom = F)
## Warning: 'config' objects don't have these attributes: 'scrollzoom'
## Valid attributes include:
## 'autosizable', 'displaylogo', 'displayModeBar', 'doubleClick', 'doubleClickDelay', 'editable', 'edits', 'fillFrame', 'frameMargins', 'globalTransforms', 'linkText', 'locale', 'locales', 'logging', 'mapboxAccessToken', 'modeBarButtons', 'modeBarButtonsToAdd', 'modeBarButtonsToRemove', 'notifyOnLogging', 'plotGlPixelRatio', 'plotlyServerURL', 'queueLength', 'responsive', 'scrollZoom', 'sendData', 'setBackground', 'showAxisDragHandles', 'showAxisRangeEntryBoxes', 'showEditInChartStudio', 'showLink', 'showSendToCloud', 'showSources', 'showTips', 'staticPlot', 'toImageButtonOptions', 'topojsonURL', 'watermark'
Note
: Grafik diatas menunjukkan 10 produk penjualan yang teratas/terpopuler berdasarkan customer yang melakukan transaksi. Barang produk yang paling populer adalah REGENCY CAKESTAND 3 TIER.
Customer Shopping Habits
<- data_ecommerce7 %>% mutate(total_amount = Quantity * UnitPrice)
data_ecommerce7 library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.1.2
%>%
data_ecommerce7group_by(hourOfDay) %>%
count() %>%
arrange(desc(n)) %>%
head(20) %>%
ggplot() + geom_col(aes(y = reorder(hourOfDay,n), x = n,fill = factor(hourOfDay))) +
geom_label(aes(y = reorder(hourOfDay,n), x = n, label = n)) +
labs(title = 'Frekuensi minat jam belanja customer',
x = 'frequency',
y = 'Hour',
fill = 'hour of day') +
theme_minimal()
Note
: Dari data diatas, dapat disimpulkan bahwa banyak customer yang melakukan transaksi pada pukul 12 siang. Sedangkan waktu yang paling jarang digunakan untuk melakukan transaksi adalah pukul 6 pagi.
Top Customer
library(treemap)
## Warning: package 'treemap' was built under R version 4.1.2
treemap(data_ecommerce7,
index = c("Country"),
vSize = "Quantity",
title = "",
palette = "Set2",
border.col = "grey40")
Note
: Berdasarkan data di atas, dapat disimpulkan bahwa negara UK memiliki customer yang paling banyak melakukan transaksi. NEgara terbesar kedua adalah Netherlands.
Number of Transactions by Day of the Week
<- data_ecommerce7 %>%
weekdaySummary group_by(Invoice_Date, dayOfWeek, month) %>%
summarise(revenue = sum(total_amount), transactions = n_distinct(InvoiceNo)) %>%
mutate(aveOrdVal = (round((revenue / transactions),2))) %>%
ungroup()
## `summarise()` has grouped output by 'Invoice_Date', 'dayOfWeek'. You can override using the `.groups` argument.
head(weekdaySummary, n = 10)
ggplot(weekdaySummary, aes(x = dayOfWeek, y = transactions)) + geom_boxplot(fill = c("steelblue1", "gold3", "orangered3","red","green","grey")) + labs(x = 'Day of the Week', y = 'Number of Daily Transactions', title = 'Number of Transactions by Day of the Week')
Note
: Dari data yang telah diolah, dapat di simpulkan bahwa banyak customer yang melakukan transaksi pada hari Kamis.
Total Transactions by Order per Month
<- weekdaySummary %>%
weekdaySummary1 mutate(yearmonth = as.yearmon(Invoice_Date),
yearmonth = format(yearmonth))%>%
group_by(Invoice_Date,yearmonth) %>%
ungroup()
library(dplyr) # untuk manipulasi data
library(ggplot2) # untuk visualisasi
library(scales) # menentukan jeda atau label secara otomatis
# menghitung gaji rata-rata untuk setiap jabatan
<- weekdaySummary1 %>%
plotdata group_by(yearmonth)%>%
mutate(n = sum(transactions))
%>%
plotdata::unite("yearmonth",
tidyr:n,
yearmonthsep=":",
remove=FALSE)%>%
treemap(.,
index=c("yearmonth"),
vSize = "n",
type="index",
palette = "Set2",
title="Total Transactions by Order per Month",
fontsize.title = 14)
Note
: Dari data penjualan yang telah di olah, dapat disimpulkan bahwa total transaksi yang telah dilakukan customer banyak dilakukan pada bulan November tahun 2011. Total transaksi yang dilakukan adalah 2751 transaksi.