Dataset Superstore Simple

Pada artikel ini, akan membahas mengenai self assessment pada programming foundation with R Language oleh Skill Academy. Adapun kolom-kolom pada dataset Superstore Simple antara lain:

Persiapan

Library

if(!require(readr)) install.packages("readr")
if(!require(dplyr)) install.packages("dplyr")
if(!require(ggplot2)) install.packages("ggplot2")

library(readr)
library(dplyr)
library(ggplot2)

Terdapat tiga buah library yang diperlukan dalam tutorial ini, antara lain:

  1. readr : berguna untuk import data dari tabular data file (csv, text file, dsb).
  2. dplyr :package yang sangat berguna untuk melakukan manipulasi/transformasi data menggunakan R.
  3. ggplot2 : salah satu package yang sangat banyak digunakan oleh pengguna R untuk kebutuhan visualisasi.

Import Dataset

Data yang kita miliki memiliki format .csv. Untuk megimport data tersebut, kita dapat menggunakan fungsi read_csv dari library readr.

Dataset_superstore_simple <- read_csv("F:/Belajar RStudio/Dataset_superstore_simple.csv")

Untuk mengecek 10 observasi awal dataset tersebut, jalankan sintaks berikut:

Dataset_superstore_simple
## # A tibble: 9,988 x 9
##    order_id order_date customer_id segment category sub_category  sales quantity
##    <chr>    <date>     <chr>       <chr>   <chr>    <chr>         <dbl>    <dbl>
##  1 CA-2016~ 2016-11-08 CG-12520    Consum~ Furnitu~ Bookcases    262.          2
##  2 CA-2016~ 2016-11-08 CG-12520    Consum~ Furnitu~ Chairs       732.          3
##  3 CA-2016~ 2016-06-12 DV-13045    Corpor~ Office ~ Labels        14.6         2
##  4 US-2015~ 2015-10-11 SO-20335    Consum~ Furnitu~ Tables       958.          5
##  5 US-2015~ 2015-10-11 SO-20335    Consum~ Office ~ Storage       22.4         2
##  6 CA-2014~ 2014-06-09 BH-11710    Consum~ Furnitu~ Furnishings   48.9         7
##  7 CA-2014~ 2014-06-09 BH-11710    Consum~ Office ~ Art            7.28        4
##  8 CA-2014~ 2014-06-09 BH-11710    Consum~ Technol~ Phones       907.          6
##  9 CA-2014~ 2014-06-09 BH-11710    Consum~ Office ~ Binders       18.5         3
## 10 CA-2014~ 2014-06-09 BH-11710    Consum~ Office ~ Appliances   115.          5
## # ... with 9,978 more rows, and 1 more variable: profit <dbl>

Ringkasan Data

glimpse(Dataset_superstore_simple)
## Rows: 9,988
## Columns: 9
## $ order_id     <chr> "CA-2016-152156", "CA-2016-152156", "CA-2016-138688", ...
## $ order_date   <date> 2016-11-08, 2016-11-08, 2016-06-12, 2015-10-11, 2015-...
## $ customer_id  <chr> "CG-12520", "CG-12520", "DV-13045", "SO-20335", "SO-20...
## $ segment      <chr> "Consumer", "Consumer", "Corporate", "Consumer", "Cons...
## $ category     <chr> "Furniture", "Furniture", "Office Supplies", "Furnitur...
## $ sub_category <chr> "Bookcases", "Chairs", "Labels", "Tables", "Storage", ...
## $ sales        <dbl> 261.9600, 731.9400, 14.6200, 957.5775, 22.3680, 48.860...
## $ quantity     <dbl> 2, 3, 2, 5, 2, 7, 4, 6, 3, 5, 9, 4, 3, 3, 5, 3, 6, 2, ...
## $ profit       <dbl> 41.9136, 219.5820, 6.8714, -383.0310, 2.5164, 14.1694,...
summary(Dataset_superstore_simple)
##    order_id           order_date         customer_id          segment         
##  Length:9988        Min.   :2014-01-03   Length:9988        Length:9988       
##  Class :character   1st Qu.:2015-05-23   Class :character   Class :character  
##  Mode  :character   Median :2016-06-26   Mode  :character   Mode  :character  
##                     Mean   :2016-04-29                                        
##                     3rd Qu.:2017-05-14                                        
##                     Max.   :2017-12-30                                        
##    category         sub_category           sales             quantity     
##  Length:9988        Length:9988        Min.   :   0.444   Min.   : 1.000  
##  Class :character   Class :character   1st Qu.:  17.248   1st Qu.: 2.000  
##  Mode  :character   Mode  :character   Median :  54.376   Median : 3.000  
##                                        Mean   : 221.953   Mean   : 3.789  
##                                        3rd Qu.: 209.814   3rd Qu.: 5.000  
##                                        Max.   :9892.740   Max.   :14.000  
##      profit         
##  Min.   :-3839.990  
##  1st Qu.:    1.730  
##  Median :    8.653  
##  Mean   :   27.105  
##  3rd Qu.:   29.345  
##  Max.   : 4946.370

Carilah customer_id yang memiliki sales paling besar!

Dari ringkasan data summary terlihat bahwa variabel sales memiliki nilai max sebesar 9892.740, sehingga kita bisa mencari customer_id yang memiliki sales tersebut.

Dataset_superstore_simple %>% filter(sales==9892.740) %>% select(customer_id, sales)
## # A tibble: 1 x 2
##   customer_id sales
##   <chr>       <dbl>
## 1 AB-10105    9893.

Dari hasil output di atas diperoleh customer_id AB-10105 memiliki sales paling besar.

Sub-category apa saja yang ada di dalam category 'Office Supplies' dan masing-masing berapa banyak total profitnya?

Dataset_superstore_simple %>% filter(category =="Office Supplies") %>% group_by(sub_category) %>% summarise(total_profit = sum(profit))
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 9 x 2
##   sub_category total_profit
##   <chr>               <dbl>
## 1 Appliances         18138.
## 2 Art                 6528.
## 3 Binders            30222.
## 4 Envelopes           6964.
## 5 Fasteners            950.
## 6 Labels              5546.
## 7 Paper              34054.
## 8 Storage            21279.
## 9 Supplies           -1189.

Berapa banyak order yang menghasilkan profit negatif (rugi)?

Dataset_superstore_simple %>% filter(profit < 0)
## # A tibble: 1,869 x 9
##    order_id order_date customer_id segment category sub_category  sales quantity
##    <chr>    <date>     <chr>       <chr>   <chr>    <chr>         <dbl>    <dbl>
##  1 US-2015~ 2015-10-11 SO-20335    Consum~ Furnitu~ Tables       9.58e2        5
##  2 US-2015~ 2015-11-22 HP-14815    Home O~ Office ~ Appliances   6.88e1        5
##  3 US-2015~ 2015-11-22 HP-14815    Home O~ Office ~ Binders      2.54e0        3
##  4 US-2017~ 2017-07-16 SF-20065    Consum~ Furnitu~ Chairs       7.14e1        2
##  5 US-2015~ 2015-09-17 TB-21520    Consum~ Furnitu~ Bookcases    3.08e3        7
##  6 US-2015~ 2015-09-17 TB-21520    Consum~ Office ~ Binders      9.62e0        2
##  7 US-2015~ 2015-09-17 TB-21520    Consum~ Office ~ Binders      6.86e0        6
##  8 CA-2016~ 2016-12-08 GH-14485    Corpor~ Furnitu~ Furnishings  1.91e2        5
##  9 CA-2015~ 2015-12-27 SN-20710    Home O~ Furnitu~ Bookcases    5.32e2        3
## 10 CA-2015~ 2015-12-27 SN-20710    Home O~ Furnitu~ Chairs       2.12e2        3
## # ... with 1,859 more rows, and 1 more variable: profit <dbl>

Berdasarkan output yang dihasilkan, terdapat 1869 order yang menghasilkan profit negatif (rugi)

Antara 3 customer_id ini, mana yang total sales-nya paling banyak: JE-16165, KH-16510, AD-10180?

Dataset_superstore_simple %>% group_by(customer_id) %>% summarise(total_sales = sum(sales)) %>% filter(customer_id == "JE-16165" |customer_id == "KH-16510" |customer_id == "AD-10180") 
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 3 x 2
##   customer_id total_sales
##   <chr>             <dbl>
## 1 AD-10180          6107.
## 2 JE-16165          2697.
## 3 KH-16510          5953.

Dari output yang dihasilkan, terlihat bahwa customer_id AD-10180 memiliki total sales paling banyak dibandingkan customer_id KH-16510 dan JE-16165.

Buatlah data frame bernama 'yearly_sales' yang berisi total sales, jumlah customers, dan total profit tiap tahun. Tahun berapa profit tertinggi diperoleh?

Dataset_superstore_simple$order_date <- as.Date(Dataset_superstore_simple$order_date)
Dataset_superstore_simple$order_year<- as.Date(cut(Dataset_superstore_simple$order_date,breaks='year'))

yearly_sales <- Dataset_superstore_simple %>% group_by(order_year) %>% summarise(sales=sum(sales),profit=sum(profit),jumlah_customer=n()) 
## `summarise()` ungrouping output (override with `.groups` argument)
yearly_sales
## # A tibble: 4 x 4
##   order_year   sales profit jumlah_customer
##   <date>       <dbl>  <dbl>           <int>
## 1 2014-01-01 461609. 51355.            1992
## 2 2015-01-01 470533. 61619.            2102
## 3 2016-01-01 587206. 79995.            2585
## 4 2017-01-01 697515. 77759.            3309

Dari ouput yang dihasilkan, tahun 2016 merupakan tahun yang memperoleh profit tertinggi.

Buatlah scatterplot antara sales dan profit untuk data di tahun 2014-2015 saja, bedakan warnanya antara tahun 2014 dan tahun 2015. Beri judul 'Sales vs Profit 2014-2015'!

yearly_2014_2015 <- Dataset_superstore_simple %>% select(order_year,sales,profit) %>% filter(order_year=="2014-01-01" | order_year=="2015-01-01")

ggplot(yearly_2014_2015,aes(x=sales,y=profit)) + geom_point (aes(color = order_year)) + labs(title = "Sales vs Profit 2014-2015")

Pada scatterplot yang dihasilkan, order_year untuk tahun 2014 ditandai dengan warna biru tua sedangkan order_year untuk tahun 2015 ditandai dengan warna biru muda.

Buatlah barchart yang berisi total profit dari 10 customer dengan total sales tertinggi di tahun 2015!

yearly_2015 <- Dataset_superstore_simple %>% select(order_year,customer_id,sales,profit) %>% filter(order_year=="2015-01-01" & sales > 0) %>% group_by(customer_id) %>% summarise(sales=sum(sales),profit=sum(profit)) %>% arrange(desc(sales))
## `summarise()` ungrouping output (override with `.groups` argument)
yearly_2015
## # A tibble: 573 x 3
##    customer_id sales profit
##    <chr>       <dbl>  <dbl>
##  1 PF-19120    9022.  -632.
##  2 KD-16495    7039.  2810.
##  3 CM-12385    6682.  3197.
##  4 FH-14365    6056.  2140.
##  5 NW-18400    5511.  1654.
##  6 SB-20185    5414.   939.
##  7 JH-15985    5353. -1429.
##  8 HM-14860    5328.  2451.
##  9 CB-12025    4663.   223.
## 10 QJ-19255    4410.  1014.
## # ... with 563 more rows
tahun2015_sales10tertinggi <- yearly_2015 %>% filter(sales > 4300)

tahun2015_sales10tertinggi
## # A tibble: 10 x 3
##    customer_id sales profit
##    <chr>       <dbl>  <dbl>
##  1 PF-19120    9022.  -632.
##  2 KD-16495    7039.  2810.
##  3 CM-12385    6682.  3197.
##  4 FH-14365    6056.  2140.
##  5 NW-18400    5511.  1654.
##  6 SB-20185    5414.   939.
##  7 JH-15985    5353. -1429.
##  8 HM-14860    5328.  2451.
##  9 CB-12025    4663.   223.
## 10 QJ-19255    4410.  1014.
ggplot(tahun2015_sales10tertinggi,aes(x=profit,y=sales)) + geom_bar(stat='identity',aes(fill=customer_id)) + labs(title = "10 customer dengan total sales tertinggi tahun 2015")