{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"collapsed":false},"outputs":[],"source":"# Grupo Bimpo competition\n# Understanding the data\n# use data.table to efficient import of large data file\nlibrary(data.table)\ntrain=fread(\"train.csv\")\n# understand the training data\n#\nstr(train)\n# How many transactions are there in total?\nnrow(train)\n# 74,180,464 transactions\n\n# There are 3-9 semanas in the data,  how many transactions in semana 3?\n# Highest number of transactions in which month? The third semana\ntable(train$Semana)\n# 3        4        5        6        7        8        9 \n# 11165207 11009593 10615397 10191837 10382849 10406868 10408713 \n\n# which week had the highest demand?\ntapply(train$Demanda_uni_equil,train$Semana,sum)\n# 3        4        5        6        7        8        9 \n# 77664309 79618866 77610637 73851129 76597014 75525105 75054450 \n# highest demanda_uni_equil in semana 4\n\n# What is the product with highest demand?\nsort(tapply(train$Demanda_uni_equil,train$Producto_ID,sum))\nprod_tabla=read.csv(\"producto_tabla.csv\")\nstr(prod_tabla)\nprod_tabla$NombreProducto[2425]\n# 100pct Whole Wheat 680g MTA ORO 43111 ... Wonderbutter 680g WON 44552\n\n# Which product had the highest demand in week 3?\nweek3 = subset(train,train$Semana==3)\nstr(week3)\nsort(tapply(week3$Demanda_uni_equil,week3$Producto_ID,sum))\n# index 2425 which is same as most sold product from all weeks\n\n# How about same but week 9?\nweek9 = subset(train,train$Semana==9)\nsort(tapply(week9$Demanda_uni_equil,week9$Producto_ID,sum))\n# same product\n\n# Consider the town, state of\n# use the colClasses option to prevent conversion to factors of \n# town and state columns\ntown_state=read.csv(\"town_state.csv\",colClasses = \"character\")\nstr(town_state)\n# What is town, state with highest demand?\nsort(tapply(train$Demanda_uni_equil,train$Agencia_ID,sum))\n# index 1911 of Agencia_ID has highest demand\ntown_state$Town[which(town_state$Agencia_ID==1911)]\ntown_state$State[which(town_state$Agencia_ID==1911)]\n# 2309 NORTE JALISCO\n# The Wonder Wheat Wonder Butter again\n\n# Consider client nombre, which client had highest demand?\nclient_tabla=read.csv(\"cliente_tabla.csv\",colClasses = \"character\")\nstr(client_tabla)\nwhich.max((tapply(train$Demanda_uni_equil,train$Cliente_ID,sum)))\n# index is 653378\ntapply(train$Demanda_uni_equil,train$Cliente_ID==653378,sum)\nclient_tabla$NombreCliente[which(client_tabla$Cliente_ID==653378)]\n# PUEBLA REMISION\n# What is percentage of demand at this cliente compared to all others\n# 17866224/518055286 = 3.44%\n\n\n# What is the highest demand product in this store?\nnorte_jalisco = subset(train,train$Agencia_ID==1911)\nstr(norte_jalisco)\nsort(tapply(norte_jalisco$Demanda_uni_equil,norte_jalisco$Producto_ID,sum))\n\n\n"}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"}},"nbformat":4,"nbformat_minor":0}