BKM Assignment

1.BKM Dataset

BKM data includes the numerical values for transactions made from credit card and debit card, including the number of transactions and transaction amount.
Loading required libraries for analysis
library(rvest)
library(tidyverse)
library(reshape2)
all_data <- lst()
for (month in 1:6) {
html_string <- paste("https://bkm.com.tr/secilen-aya-ait-sektorel-gelisim/?filter_year=2019&filter_month=",
                     month,sep='')
bkm_sektor <- read_html(html_string)
bkm_table <- html_nodes(bkm_sektor, "table")
my_table <- html_table(bkm_table[4],fill = TRUE)  %>%  as.data.frame() %>% slice(3:max(nrow(.)-1)) 
my_table <- my_table %>% mutate(MONHT=month)
all_data <- rbind(all_data,my_table)
}


colnames(all_data) <- c("work_group", "cc_sales_volume", 
                        "bc_sales_volume", "cc_trans_cost", "bc_trans_cost","MONTH")

all_data$cc_sales_volume <- as.numeric(str_replace_all(all_data$cc_sales_volume, pattern=fixed("."), ""))
all_data$bc_sales_volume <- as.numeric(str_replace_all(all_data$bc_sales_volume, pattern=fixed("."), ""))

all_data$cc_trans_cost<-str_replace_all(all_data$cc_trans_cost, pattern=fixed("."), "")
all_data$cc_trans_cost<-as.numeric(str_replace_all(all_data$cc_trans_cost, pattern=fixed(","), "."))

all_data$bc_trans_cost<-str_replace_all(all_data$bc_trans_cost, pattern=fixed("."), "")
all_data$bc_trans_cost<-as.numeric(str_replace_all(all_data$bc_trans_cost, pattern=fixed(","), "."))

head(all_data)
##                               work_group cc_sales_volume bc_sales_volume cc_trans_cost bc_trans_cost MONTH
## 1                         ARABA KIRALAMA          256372           49296        195.13         14.77     1
## 2 ARAÇ KIRALAMA-SATIS/SERVIS/YEDEK PARÇA         2967019          642136       2185.84        127.16     1
## 3           BENZIN VE YAKIT ISTASYONLARI        25277186         8684036       5066.04        680.01     1
## 4                     BIREYSEL EMEKLILIK         2271587             697        716.42          0.30     1
## 5                           ÇESITLI GIDA        28362091        15221891       4473.98        673.70     1
## 6                     DOGRUDAN PAZARLAMA          757602           40038        678.99          7.81     1

Analysis 1 - Calculating Unit Price of Transactions and Monthly Comparision (BIREYSEL EMEKLILIK)

Calculating unit price of transactions by Credit Card and Bank Card. Then using ggplot to monthly comparision.

analysis_1_CC <- all_data %>% filter(work_group == "BIREYSEL EMEKLILIK") %>% mutate(UNITPRICE=cc_trans_cost*1000000/cc_sales_volume, type='Credit Card')

analysis_1_BC <- all_data %>% filter(work_group == "BIREYSEL EMEKLILIK") %>% mutate(UNITPRICE=bc_trans_cost*1000000/bc_sales_volume, type='Bank Card')


analysis_1 <- rbind(analysis_1_CC,analysis_1_BC)

print(analysis_1)
##            work_group cc_sales_volume bc_sales_volume cc_trans_cost bc_trans_cost MONTH UNITPRICE        type
## 1  BIREYSEL EMEKLILIK         2271587             697        716.42          0.30     1  315.3830 Credit Card
## 2  BIREYSEL EMEKLILIK         2075842            1106        656.63          0.41     2  316.3198 Credit Card
## 3  BIREYSEL EMEKLILIK         2280331            1474        753.15          0.61     3  330.2810 Credit Card
## 4  BIREYSEL EMEKLILIK         2142251            1445        700.94          0.55     4  327.1979 Credit Card
## 5  BIREYSEL EMEKLILIK         2202538            1566        736.35          0.62     5  334.3189 Credit Card
## 6  BIREYSEL EMEKLILIK         2060390            1256        686.98          0.42     6  333.4223 Credit Card
## 7  BIREYSEL EMEKLILIK         2271587             697        716.42          0.30     1  430.4161   Bank Card
## 8  BIREYSEL EMEKLILIK         2075842            1106        656.63          0.41     2  370.7052   Bank Card
## 9  BIREYSEL EMEKLILIK         2280331            1474        753.15          0.61     3  413.8399   Bank Card
## 10 BIREYSEL EMEKLILIK         2142251            1445        700.94          0.55     4  380.6228   Bank Card
## 11 BIREYSEL EMEKLILIK         2202538            1566        736.35          0.62     5  395.9132   Bank Card
## 12 BIREYSEL EMEKLILIK         2060390            1256        686.98          0.42     6  334.3949   Bank Card
ggplot(analysis_1, aes(x=MONTH, y=UNITPRICE, fill=type))+geom_bar(stat="identity",position="dodge")+labs(x="Month",y="Unit Price of Transaction",title="Unit Price of Transactions and Monthly Comparision(BIREYSEL EMEKLILIK)")

### Analysis 2 - The Distribution of Total Expenditures by Work Groups

analysis_2 <- all_data %>% select(MONTH,work_group,cc_sales_volume,bc_sales_volume) %>% mutate(Sales_sum = cc_sales_volume+bc_sales_volume) 

analysis_2 <- analysis_2 %>% group_by(work_group)%>%  summarise(Sales_sum=sum(Sales_sum)) %>%mutate(Sales_sum, sales_percentage = Sales_sum / sum(Sales_sum) * 100)%>%   arrange(desc(Sales_sum))

Top_Five_Sector<-head(analysis_2,5)
print(Top_Five_Sector)
## # A tibble: 5 x 3
##   work_group                     Sales_sum sales_percentage
##   <chr>                              <dbl>            <dbl>
## 1 MARKET VE ALISVERIS MERKEZLERI 957742221            30.9 
## 2 YEMEK                          486166406            15.7 
## 3 ÇESITLI GIDA                   286302262             9.24
## 4 BENZIN VE YAKIT ISTASYONLARI   228279355             7.36
## 5 GIYIM VE AKSESUAR              226946321             7.32
ggplot(Top_Five_Sector, aes(x="", y=sales_percentage, fill=factor(work_group))) + geom_bar(stat="identity", width=1) + coord_polar("y", start=0) +geom_text(aes(label = paste0(round(sales_percentage), "%")), position = position_stack(vjust = 0.5)) + labs(x = NULL, y = NULL, fill = NULL, title = "Sales Percentage Over Top Five Work Group") + theme_classic() + theme(axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
          plot.title = element_text(hjust = 0.5, color = "#666666"))