# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function
library(data.table)
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory


system("ls ../input")


# Grupo Bimpo competition
# Understanding the data
# use data.table to efficient import of large data file
library(data.table)
train=fread("../input/train.csv")
# understand the training data
#
str(train)
# How many transactions are there in total?
nrow(train)
# 74,180,464 transactions

# There are 3-9 semanas in the data,  how many transactions in semana 3?
# Highest number of transactions in which month? The third semana
table(train$Semana)
# 3        4        5        6        7        8        9 
# 11165207 11009593 10615397 10191837 10382849 10406868 10408713 

# which week had the highest demand?
tapply(train$Demanda_uni_equil,train$Semana,sum)
# 3        4        5        6        7        8        9 
# 77664309 79618866 77610637 73851129 76597014 75525105 75054450 
# highest demanda_uni_equil in semana 4

# What is the product with highest demand?
sort(tapply(train$Demanda_uni_equil,train$Producto_ID,sum))
prod_tabla=read.csv("../input/producto_tabla.csv")
str(prod_tabla)
prod_tabla$NombreProducto[2425]
# 100pct Whole Wheat 680g MTA ORO 43111 ... Wonderbutter 680g WON 44552

# Which product had the highest demand in week 3?
week3 = subset(train,train$Semana==3)
str(week3)
sort(tapply(week3$Demanda_uni_equil,week3$Producto_ID,sum))
# index 2425 which is same as most sold product from all weeks

# Consider the town, state of
# use the colClasses option to prevent conversion to factors of 
# town and state columns
town_state=read.csv("../input/town_state.csv",colClasses = "character")
str(town_state)
# What is town, state with highest demand?
sort(tapply(train$Demanda_uni_equil,train$Agencia_ID,sum))
# index 1911 of Agencia_ID has highest demand
town_state$Town[which(town_state$Agencia_ID==1911)]
town_state$State[which(town_state$Agencia_ID==1911)]
# 2309 NORTE JALISCO

# Uncomment this portion of script, commented because exceeded memory limit
# Consider client nombre, which client had highest demand?
# client_tabla=read.csv("../input/cliente_tabla.csv",colClasses = "character")
# str(client_tabla)
# which.max((tapply(train$Demanda_uni_equil,train$Cliente_ID,sum)))
# index is 653378
# tapply(train$Demanda_uni_equil,train$Cliente_ID==653378,sum)
# client_tabla$NombreCliente[which(client_tabla$Cliente_ID==653378)]
# PUEBLA REMISION
# What is percentage of demand at this cliente compared to all others
# 17866224/518055286 = 3.44%


# Any results you write to the current directory are saved as output.