In this section, you are expected to be more confident to create your own function. Here I advise you to create a function for each tasks bellow:
## [1] 7.5
## [1] 5.5
Middle_value_genap<- function(x)
{
1/2*((length(x)/2)+((length(x)+1)/2))
}
x<- c(6,7,8,9,10)
Middle_value_genap(x)## [1] 2.75
list<- c(6,7,8,9,10,11,12,13,14)
most_frequent<- function(x)
{
y<- data.frame(table(x))
y[y$Freq==max(y$Freq),1]
}
most_frequent(list)## [1] 6 7 8 9 10 11 12 13 14
## Levels: 6 7 8 9 10 11 12 13 14
## [1] 10
## [1] 6
variance<-function(fs)
{
test_f1<-(f-(sum(f)/length(f)))^2
sum(test_f1)/(length(f)) #For Sample Variance
}
f<-c(1,2,3,4,5,6)
variance(f)## [1] 2.916667
standard_deviation<- function(x)
{
sqrt(((length(x)*sum(x^2)-sum(x)^2))/length(x)*length(x-1))
}
x <- c(101,102,103,104,105,106,107)
standard_deviation(x)## [1] 14
pencilan<-function(h)
{
q1<-quantile(h)[2]
q3<-quantile(h)[4]
jarak<-q3-q1
atas<-(jarak*1.5)+q3
bawah<- q1-(jarak*1.5)
result<- (which(h<bawah|h>atas))
h[(result)]
}
h<-c(1,2,3,4,9,7,5,3,200,100,12,14)
pencilan(h)## [1] 200 100
summary (all functions) - optional
Multivariate variable (more dimension)
avarage_freq<- function(x,freq)
{
sum(x*freq)/length(x)
}
x<- c(6,7,8,9,10)
freq<- c(5,6,7,8,9)
avarage_freq(x,freq)## [1] 58
NilaiTengah<- function(l,m) {
n <- sum(m)
if(n%%2==0)
{
bentuk<-n%/%2
NilaiTengah<-((sort(rep.int(l,m))[bentuk]+sort(rep.int(l,m))[bentuk+1])/2)
}
else if(n%%2==1)
{
bentuk<-(n+1)%/%2
NilaiTengah<-(sort(rep.int(l,m))[bentuk])
}
return(NilaiTengah)
TitikTengah
}
l<-c(1,3,2,4)
m<-c(7,6,7,6)
NilaiTengah(l,m)## [1] 2
Modus <- function(m,o)
{
data <- sort(rep.int(m,o))
nilai <- unique(data)
tab <- (tabulate(match(data,nilai)))
nilai[tab==max(tab)]
}
m<-c(1,2,5,3,4,5)
o<-c(10,3,12,14,5,5)
Modus(m,o)## [1] 5
nilaitertinggi<-function(x,y)
{
sortqr<-sort(rep.int(x,y))
tail(sortqr,1)
}
x<-c(2,5,3,150,140)
y<-c(2,2,2,2,2)
nilaitertinggi(x,y)## [1] 150
Minimum<-function(u,m)
{
sortqr<-sort(rep.int(u,m))
head(u,1)
}
u<-c(1,2,3,4,14)
m<-c(5,4,5,4,5)
Minimum(u,m)## [1] 1
Varians<-function(r,s)
{
sum((sort(rep.int(r,s))-((sum(r*s))/(sum(s))))^2)/(sum(s)-1)
}
r<-c(1,2,3,4,5)
s<-c(2,2,2,2,2)
Varians(r,s)## [1] 2.222222
StandarDeviasi<-function(z,x)
{
sqrt(sum((sort(rep.int(z,x))-((sum(z*x))/(sum(x))))^2)/(sum(x)-1))
}
z<-c(1,2,3,4,5)
x<-c(5,6,5,6,5)
StandarDeviasi(z,x)## [1] 1.414214
Pencilan<- function(t,x){
q1 <- quantile(sort(rep.int(t,x)))[2]
q3 <- quantile(sort(rep.int(t,x)))[4]
jarak <- q3 - q1
atas <- (jarak * 1.5)+ q3
bawah <- q1 - (jarak * 1.5)
result<- (which(sort(rep.int(t,x))<bawah|sort(rep.int(t,x))>atas))
sort(rep.int(t,x))[head(result)]
}
t<-c(7,7,7,7,200,7)
x<-c(1,4,1,1,1,1)
Pencilan(t,x)## [1] 200
Id <- (1:5000)
Date <- seq(as.Date("2018/01/01"), by = "day", length.out = 5000)
Name <- sample(c("Angel","Sherly","Vanessa","Irene","Julian","Jeffry","Nikita","Kefas","Siana","Lala",
"Fallen","Ardifo","Kevin","Michael","Felisha","Calisha","Patricia","Naomi","Eric","Jacob"),
5000, replace = T)
City <- sample(rep(c("Jakarta","Bogor","Depok","Tangerang","Bekasi"), times = 1000))
Outlet <- sample(c("Outlet 1","Outlet 2","Outlet 3","Outlet 4","Outlet 5"),5000, replace = T)
Menu <- c("Cappucino","Es Kopi Susu","Hot Caramel Latte","Hot Chocolate","Hot Red Velvet Latte","Ice Americano",
"Ice Berry Coffee","Ice Cafe Latte","Ice Caramel Latte","Ice Coffee Avocado","Ice Coffee Lite",
"Ice Matcha Espresso","Ice Matcha Latte","Ice Red Velvet Latte")
all_menu <- sample(Menu, 5000, replace = T)
Price <- sample(18000:45000,14, replace = T)
DFPrice <- data.frame(Menu, Price)
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
## Joining, by = "Menu"
## Id Date Name City Outlet Menu Price
## 1 1 2018-01-01 Michael Tangerang Outlet 4 Ice Coffee Lite 35385
## 2 2 2018-01-02 Julian Bekasi Outlet 3 Hot Chocolate 28881
## 3 3 2018-01-03 Calisha Jakarta Outlet 3 Hot Caramel Latte 24600
## 4 4 2018-01-04 Nikita Tangerang Outlet 5 Es Kopi Susu 36929
## 5 5 2018-01-05 Sherly Bekasi Outlet 1 Ice Coffee Avocado 36972
Let’s say, you have a data set already in your hand as you can see above. Please create a function to calculate the following tasks:
City_freq<-data.frame(prop.table(table(KopiKenangan$City)*100))
addPercent <- function(x)
{
percent <- round(City_freq[,2] * 100, digits = 1)
result <- paste(percent, sep = "", "%")
return(result)
}
persen<-addPercent(x)
City_freq$Freq<-NULL
cbind(City_freq,persen)## Var1 persen
## 1 Bekasi 20%
## 2 Bogor 20%
## 3 Depok 20%
## 4 Jakarta 20%
## 5 Tangerang 20%
##
## Cappucino Es Kopi Susu Hot Caramel Latte Hot Chocolate
## Angel 10 25 13 15
## Ardifo 18 22 16 13
## Calisha 21 16 15 16
## Eric 9 15 18 22
## Fallen 22 19 20 16
## Felisha 17 15 15 23
## Irene 20 19 16 18
## Jacob 21 21 11 19
## Jeffry 14 15 21 17
## Julian 21 25 29 18
## Kefas 13 15 21 25
## Kevin 22 25 20 31
## Lala 21 19 12 21
## Michael 22 16 17 16
## Naomi 24 26 20 20
## Nikita 21 19 18 17
## Patricia 14 14 20 17
## Sherly 19 20 15 18
## Siana 11 24 29 20
## Vanessa 13 14 22 23
##
## Hot Red Velvet Latte Ice Americano Ice Berry Coffee Ice Cafe Latte
## Angel 15 12 20 15
## Ardifo 14 14 16 9
## Calisha 22 18 16 16
## Eric 21 18 20 13
## Fallen 20 18 20 17
## Felisha 14 25 17 23
## Irene 15 15 11 13
## Jacob 13 22 24 14
## Jeffry 23 14 17 22
## Julian 7 18 19 17
## Kefas 17 15 13 25
## Kevin 21 15 23 9
## Lala 18 18 16 24
## Michael 20 17 26 15
## Naomi 20 18 19 20
## Nikita 16 17 18 21
## Patricia 17 10 14 22
## Sherly 17 15 21 19
## Siana 20 19 19 24
## Vanessa 20 15 18 13
##
## Ice Caramel Latte Ice Coffee Avocado Ice Coffee Lite
## Angel 11 20 16
## Ardifo 12 17 22
## Calisha 18 17 21
## Eric 18 18 16
## Fallen 15 17 16
## Felisha 21 18 14
## Irene 12 18 20
## Jacob 24 27 22
## Jeffry 19 16 16
## Julian 15 17 21
## Kefas 21 14 9
## Kevin 27 16 11
## Lala 16 17 19
## Michael 24 22 17
## Naomi 15 13 12
## Nikita 16 9 15
## Patricia 23 17 19
## Sherly 6 23 15
## Siana 21 22 18
## Vanessa 21 14 26
##
## Ice Matcha Espresso Ice Matcha Latte Ice Red Velvet Latte
## Angel 14 17 13
## Ardifo 17 22 20
## Calisha 25 18 16
## Eric 17 21 17
## Fallen 15 15 14
## Felisha 11 16 13
## Irene 16 23 12
## Jacob 20 17 12
## Jeffry 15 16 11
## Julian 16 20 17
## Kefas 19 11 21
## Kevin 16 22 13
## Lala 24 21 14
## Michael 20 23 18
## Naomi 17 17 16
## Nikita 26 17 27
## Patricia 16 13 21
## Sherly 18 12 20
## Siana 22 20 23
## Vanessa 17 20 17