| Kontak | : \(\downarrow\) |
| dsciencelabs@outlook.com | |
| https://www.instagram.com/dsciencelabs/ | |
| RPubs | https://rpubs.com/dsciencelabs/ |
Asumsikan Anda telah mengumpulkan beberapa kumpulan data dari perusahaan ABC Property seperti yang dapat kita lihat pada tabel berikut:
Id <- (1:10000)
Marketing_Name <- rep(c("Angel","Sherly","Vanessa","Irene","Julian",
"Jeffry","Nikita","Kefas","Siana","Lala",
"Fallen","Ardifo","Kevin","Juen","Jerrel",
"Imelda","Widi","Theodora","Elvani","Jonathan",
"Sofia","Abraham","Siti","Niko","Sefli",
"Bene", "Diana", "Pupe", "Andi", "Tatha",
"Endri", "Monika", "Hans", "Debora","Hanifa",
"James", "Jihan", "Friska","Ardiwan", "Bakti",
"Anthon","Amry", "Wiwik", "Bastian", "Budi",
"Leo","Simon","Matius","Arry", "Eliando"), 200)
Work_Exp <- rep(c(1.3,2.4,2.5,3.6,3.7,4.7,5.7,6.7,7.7,7.3,
5.3,5.3,10,9.3,3.3,3.3,3.4,3.4,3.5,5.6,
3.5,4.6,4.6,5.7,6.2,4.4,6.4,6.4,3.5,7.5,
4.6,3.7,4.7,4.3,5.2,6.3,7.4,2.4,3.4,8.2,
6.4,7.2,1.5,7.5,10,4.5,6.5,7.2,7.1,7.6),200)
City <- sample(c("Jakarta","Bogor","Depok","Tengerang","Bekasi"),10000, replace = T)
Cluster <- sample(c("Victoria","Palmyra","Winona","Tiara", "Narada",
"Peronia","Lavesh","Alindra","Sweethome", "Asera",
"Teradamai","Albasia", "Adara","Neon","Arana",
"Asoka", "Primadona", "Mutiara","Permata","Alamanda" ), 10000, replace=T)
Price <- sample(c(7000:15000),10000, replace = T)
Date_Sales <- sample(seq(as.Date("2018/01/01"), by = "day", length.out = 1000),10000, replace = T)
Advertisement <- sample(c(1:20), 10000, replace = T)
Data <- data.frame(Id,
Marketing_Name,
Work_Exp,
City,
Cluster,
Price,
Date_Sales,
Advertisement)
library(DT)
datatable(Data)
Kategorikan variabel Harga pada dataset di atas menjadi tiga kelompok sebagai berikut:
Tetapkan ke dalam variabel baru yang disebut Kelas dengan menggunakan fungsi kontrol If, else if, dan else.
Data$Kelas <- ifelse(Data$Price>12000,
"High",
ifelse(Data$Price<10000,
"Low",
"Medium"))
library(DT)
datatable(Data)
Kategorikan variabel Harga pada dataset di atas menjadi enam kelompok sebagai berikut:
Tetapkan ke dalam variabel baru yang disebut Booking_fee dengan menggunakan fungsi kontrol If, else if, dan else.
Data$Booking_fee <- ifelse(Data$Price >= 13000,
Data$Price*0.1,
ifelse(Data$Price>=11000,
Data$Price*0.09,
ifelse(Data$Price>=10000,
Data$Price*0.08,
ifelse(Data$Price>=9000,
Data$Price*0.07,
ifelse(Data$Price>=8000,
Data$Price*0.06,
Data$Price*0.05)))))
library(DT)
library(dplyr)
subset(Data, select = c(6, 10)) %>% datatable()
Data$Booking_fee<- Data$Booking_fee
print(head(Data, 10))
## Id Marketing_Name Work_Exp City Cluster Price Date_Sales
## 1 1 Angel 1.3 Depok Mutiara 12782 2020-06-27
## 2 2 Sherly 2.4 Bekasi Albasia 9985 2018-01-27
## 3 3 Vanessa 2.5 Bekasi Narada 13617 2018-02-04
## 4 4 Irene 3.6 Tengerang Teradamai 12920 2018-03-18
## 5 5 Julian 3.7 Bogor Mutiara 8781 2020-09-25
## 6 6 Jeffry 4.7 Tengerang Adara 8700 2020-05-11
## 7 7 Nikita 5.7 Bekasi Narada 12606 2019-06-11
## 8 8 Kefas 6.7 Bogor Asoka 11253 2020-06-06
## 9 9 Siana 7.7 Tengerang Asoka 13136 2019-10-26
## 10 10 Lala 7.3 Tengerang Sweethome 7898 2018-12-14
## Advertisement Kelas Booking_fee
## 1 1 High 1150.38
## 2 13 Low 698.95
## 3 3 High 1361.70
## 4 18 High 1162.80
## 5 14 Low 526.86
## 6 16 Low 522.00
## 7 15 High 1134.54
## 8 12 Medium 1012.77
## 9 20 High 1313.60
## 10 16 Low 394.90
Untuk data penjualan di perusahaan ‘ABC Property’ pada nama “Bakti” sebagai berikut :
library(dplyr)
for (i in "Bakti"){
Data%>%filter(Marketing_Name==i)%>%head(10)%>%print()
break
}
## Id Marketing_Name Work_Exp City Cluster Price Date_Sales
## 1 40 Bakti 8.2 Jakarta Teradamai 7462 2018-09-20
## 2 90 Bakti 8.2 Jakarta Winona 13784 2018-12-19
## 3 140 Bakti 8.2 Jakarta Primadona 12199 2020-04-22
## 4 190 Bakti 8.2 Jakarta Peronia 7403 2018-03-23
## 5 240 Bakti 8.2 Depok Tiara 9223 2019-03-20
## 6 290 Bakti 8.2 Bogor Primadona 11677 2019-07-16
## 7 340 Bakti 8.2 Bekasi Palmyra 14787 2018-08-12
## 8 390 Bakti 8.2 Depok Sweethome 11525 2020-01-03
## 9 440 Bakti 8.2 Jakarta Tiara 11291 2020-08-05
## 10 490 Bakti 8.2 Tengerang Victoria 11841 2018-09-09
## Advertisement Kelas Booking_fee
## 1 2 Low 373.10
## 2 17 High 1378.40
## 3 10 High 1097.91
## 4 2 Low 370.15
## 5 12 Low 645.61
## 6 14 Medium 1050.93
## 7 11 High 1478.70
## 8 14 Medium 1037.25
## 9 16 Medium 1016.19
## 10 4 Medium 1065.69
Jika Anda akan mendapatkan bonus 2% dari Booking fee per unit sebagai pemasaran dan juga mendapatkan bonus tambahan 1% jika Anda telah bekerja di perusahaan ini selama lebih dari 3 tahun. Silakan hitung total bonus dengan menggunakan pernyataan if, for, dan break.
library(dplyr)
list<-c()
for (i in Data$Marketing_Name){
Data$Bonus <- ifelse(Data$Work_Exp>3, Data$Booking_fee*0.03, Data$Booking_fee*0.02)
list[[i]]<- aggregate(Bonus ~ Marketing_Name, data = Data, sum, T)
break}
result<-list[[i]]
result <- result[order(result$Bonus,decreasing=T),]
result%>%filter(Marketing_Name=="Bakti")%>%print()
## Marketing_Name Bonus
## 1 Bakti 5574.946
Pada bagian ini, Anda diharapkan dapa membuat fungsi yang dapat menjawab setiap penyataan dibawah ini dengan melibatkan setiap fungsi kontrol yang dipelajari pada pertemuan 7.
library(dplyr)
library(tidyverse)
library(dplyr)
list_by_Price <-aggregate(Price~ Marketing_Name , data = Data, sum)
result_price <-list_by_Price[order(list_by_Price$Price,decreasing=T),]
list_by_adv <-aggregate(Advertisement~ Marketing_Name , data = Data, sum)
Datasales <- data.frame(left_join(result_price, list_by_adv))%>%head(1)
Datasales
## Marketing_Name Price Advertisement
## 1 Amry 2274538 2183
library(dplyr)
Profitable_City_Cluster <- aggregate ( Price ~ Cluster + City, data= Data, sum)
Data_City_Cluster <-Profitable_City_Cluster[order(Profitable_City_Cluster$Price,decreasing=T),]%>%head(1)
Data_City_Cluster
## Cluster City Price
## 86 Asera Tengerang 1389003
Data$Advertisement <- as.numeric(Data$Advertisement)
Data$Advertisement_Cost <- Data$Advertisement * 4
Total_Cost <- sum(Data$Advertisement_Cost)
paste("The total cost was", Total_Cost)
## [1] "The total cost was 418604"
Data$Advertisement.cost <- Data$Advertisement*4
aggregate(Advertisement.cost ~ Marketing_Name, data= Data, mean)
## Marketing_Name Advertisement.cost
## 1 Abraham 39.44
## 2 Amry 43.66
## 3 Andi 40.66
## 4 Angel 43.22
## 5 Anthon 41.90
## 6 Ardifo 40.38
## 7 Ardiwan 45.32
## 8 Arry 38.34
## 9 Bakti 41.06
## 10 Bastian 42.62
## 11 Bene 40.24
## 12 Budi 44.10
## 13 Debora 41.80
## 14 Diana 40.44
## 15 Eliando 41.52
## 16 Elvani 42.54
## 17 Endri 44.06
## 18 Fallen 47.12
## 19 Friska 41.56
## 20 Hanifa 40.80
## 21 Hans 42.44
## 22 Imelda 42.50
## 23 Irene 44.98
## 24 James 43.14
## 25 Jeffry 40.22
## 26 Jerrel 44.02
## 27 Jihan 40.60
## 28 Jonathan 42.78
## 29 Juen 44.96
## 30 Julian 41.42
## 31 Kefas 41.56
## 32 Kevin 43.56
## 33 Lala 39.14
## 34 Leo 39.98
## 35 Matius 40.08
## 36 Monika 39.72
## 37 Nikita 43.98
## 38 Niko 40.84
## 39 Pupe 39.52
## 40 Sefli 42.12
## 41 Sherly 41.14
## 42 Siana 43.58
## 43 Simon 42.50
## 44 Siti 40.38
## 45 Sofia 43.76
## 46 Tatha 39.00
## 47 Theodora 42.42
## 48 Vanessa 39.26
## 49 Widi 40.52
## 50 Wiwik 42.12
library(dplyr)
library(tidyverse)
Data$Revenue <- Data$Price - Data$Booking_fee
Monthly_Rev <- Data%>%separate(Date_Sales, c("Year", "Month", "Day"), sep = "-")%>%
select(Year, Month, Revenue)
Monthly.Rev <-aggregate(Revenue ~ Month + Year, data = Monthly_Rev, sum)
Monthly.Rev
## Month Year Revenue
## 1 01 2018 3391985
## 2 02 2018 2897540
## 3 03 2018 3233762
## 4 04 2018 3151002
## 5 05 2018 3438817
## 6 06 2018 2754942
## 7 07 2018 3326189
## 8 08 2018 3219698
## 9 09 2018 2853057
## 10 10 2018 2893327
## 11 11 2018 3022508
## 12 12 2018 3436697
## 13 01 2019 3056697
## 14 02 2019 2505635
## 15 03 2019 2950020
## 16 04 2019 3481030
## 17 05 2019 3065328
## 18 06 2019 2898673
## 19 07 2019 3094487
## 20 08 2019 3207818
## 21 09 2019 2833959
## 22 10 2019 2976701
## 23 11 2019 2868009
## 24 12 2019 2977550
## 25 01 2020 3371527
## 26 02 2020 3002308
## 27 03 2020 3050490
## 28 04 2020 3084955
## 29 05 2020 3116299
## 30 06 2020 3333659
## 31 07 2020 2818103
## 32 08 2020 3144749
## 33 09 2020 2475544