1 - Data Preparation

The interbank clearing of the debts and credits of the card holders arising from their purchases is carried out within BKM’s structure through the domestic clearing and settlement of debit and credit card transactions. The main activities of BKM are; carrying out the authorization operation between the banks, developing the procedures applicable to the banks in the credit card and debit card sector, forming the domestic rules and regulations, making efforts in relation to provision of standardization and taking the relevant decisions, establishing relations with the international organizations and commissions and representing the members in these organizations when necessary and executing the ongoing bank operations from a single central operation site in a more secure, fast and cost-effective manner.

# Creating a function to filter year and month on the URL
FuncBKM <- 
  function(year,month) {
    url <- paste('https://bkm.com.tr/en/secilen-aya-ait-sektorel-gelisim/?filter_year=',year,'&filter_month=',month,'&List=Lis',sep='',collapse = NULL)
  return(url)
  }

list_all <- c('201701','201702','201703','201704','201705','201706','201707','201708','201709','201710','201711','201712','201801','201802','201803','201804','201805','201806','201807','201808','201809','201810','201811','201812','201901','201902','201903','201904','201905','201906')


raw_df_full <- ''
for (i in 1:length(list_all)) {
v_year = substr(list_all[i],1,4)
v_month = substr(list_all[i],5,6)
    url<-FuncBKM(v_year,v_month)
    
    page <- read_html(url)
    
    raw_df <- 
      page %>%
      html_nodes("table") %>%.[(4)] %>% 
      html_table(page, fill = TRUE,header = FALSE) %>% 
      as.data.frame() %>%
      slice(3:max(nrow(.)))  
    
    
    raw_df <- 
      raw_df %>% 
      mutate_if(is.numeric,funs(ifelse(is.na(.),0,.))) %>%  
      mutate(year = v_year,month = v_month)
    
    
    raw_df_full<-rbind(raw_df_full,raw_df)
}


colnames(raw_df_full) <- c('merchant_category','cc_transaction_count','dc_transaction_count','cc_transaction_amount','dc_transaction_amount','year','month')
raw_df_full <- raw_df_full %>% slice(-c(1))

2 - Analysis : Average Spent of Card Types

df_avg_spent <-
  raw_df_full %>% 
    select(year,dc_transaction_amount,cc_transaction_amount,dc_transaction_count,cc_transaction_count) %>%
    group_by(year) %>%
    summarise(Avg_Spend_of_Debitcard = sum(as.numeric((gsub(",","",dc_transaction_amount))),na.rm=T)/sum(as.numeric((gsub(",","",dc_transaction_count))),na.rm = T),
     Avg_Spend_of_Creditcard = sum(as.numeric((gsub(",","",cc_transaction_amount))),na.rm = T)/sum(as.numeric((gsub(",","",cc_transaction_count))),na.rm = T)
    )%>%
    arrange(desc(year)) %>%
    mutate(rwn =row_number()) %>%
    filter(rwn<=3)
df_avg_spent_pivot <- melt(df_avg_spent, id.vars = c("year"), 
                                    measure.vars = c("Avg_Spend_of_Debitcard", "Avg_Spend_of_Creditcard"))
ggplot(data = df_avg_spent_pivot, aes(x = year , y = value , group = variable)) + 
  geom_bar(aes(fill = year),stat = "identity") + scale_fill_hue() + theme(axis.text.x = element_text(angle = 30, hjust = 1))+
  labs(fill = "Year") + 
  facet_grid(~ variable) +
  scale_y_continuous("Spend") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))