{"metadata": {"kernelspec": {"display_name": "R", "name": "ir", "language": "R"}, "language_info": {"mimetype": "text/x-r-source", "version": "3.4.1", "name": "R", "pygments_lexer": "r", "file_extension": ".r", "codemirror_mode": "r"}}, "nbformat_minor": 1, "nbformat": 4, "cells": [{"source": ["***Introduction***\n", "\n", "As a retailer, Once you get  access to sales data it is very important to understand how that data plays a role in your business\u2019 success.It may help you in getting more  information on your best selling products and may be used to make smarter purchasing decisions, improve customer satisfaction, and increase sales. \n", "\n", "Keeping this in mind we are planing to to showcase the top selling items by total unit sales year on year."], "metadata": {"_cell_guid": "8819345a-b284-43b2-85b8-902ef7446611", "_uuid": "9397243415c29a685ac0e960cd2b7207f7191cea"}, "cell_type": "markdown"}, {"source": ["library(data.table)\n", "library(dplyr)\n", "library(lubridate)\n", "library(ggplot2)"], "metadata": {"_cell_guid": "ca338fee-cc81-484f-b6d0-4276cf47daa1", "_uuid": "00f948d8846dc2adfa1aa869b30c476359eaa8eb"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***Reading Train and Item data ***"], "metadata": {"_cell_guid": "be09d49d-73ab-4f2a-8727-f7067932a524", "_uuid": "2b9ecf10918c0bb6167d1620eda463e46436f633"}, "cell_type": "markdown"}, {"source": ["itemsData = fread(\"../input/items.csv\",header = T)\n", "trainData = fread(\"../input/train.csv\",header = T)"], "metadata": {"_cell_guid": "5f0f3c7d-369a-420c-9f98-01dc76b34fec", "_uuid": "29c1b8f9884bcbff81d1e39c5103ab1dfcaf764b"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["trainData_2013 <- trainData[1:16322662,]\n", "trainData_2014 <- trainData[16322663:38594264,]\n", "trainData_2015 <- trainData[38594265:66458908,]\n", "trainData_2016 <- trainData[66458909:101688779,]\n", "trainData_2017 <- trainData[101688780:125497040,]\n", "rm(trainData)"], "metadata": {"_cell_guid": "dc16182b-1517-45a3-a69d-09eef4004ed4", "_uuid": "11b2879ddbf074ff5f196fee203ace13213e97ff"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["**Mearging training data witl item data and basic pre-processing **"], "metadata": {"_cell_guid": "99705f2d-7d81-4542-8167-9a6ddc58d122", "_uuid": "e6e92ae44a876ff3006af5982df87186463c17be"}, "cell_type": "markdown"}, {"source": ["*** FOR YEAR 2013***"], "metadata": {"_cell_guid": "4824e0b9-3f6e-4020-941a-b7ccac26375c", "_uuid": "e1323f9eb688718c43aef814e4fc204225527011"}, "cell_type": "markdown"}, {"source": ["trainData_2013 <- merge(trainData_2013,itemsData, by = c(\"item_nbr\"))\n", "trainData_2013$year <- year(as.POSIXlt(trainData_2013$date, format=\"%Y-%m-%d\"))\n", "trainData_2013$item_nbr<- as.factor(trainData_2013$item_nbr)\n", "trainData_2013$year <- as.factor(trainData_2013$year)"], "metadata": {"_cell_guid": "c12822d1-8656-4544-a1cd-fdf674326e45", "_uuid": "3ecdbd0a99c0599d92627e4267477eb1d4691278"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***10 Top items In YEAR 2013 by total unit sales***"], "metadata": {"_cell_guid": "d5d3d3bf-63c2-48e5-b21b-e75a1d6c4aa7", "_uuid": "89b3fc20fd4ec703b7e9f172ec764801126a82e4"}, "cell_type": "markdown"}, {"source": ["x = data.frame(trainData_2013 %>% group_by(item_nbr,year) %>% summarise(tot_Unit_Sold = sum(unit_sales)))\n", "group <- as.data.table(x)\n", "topItemsEachYear = data.frame(group %>% group_by(year) %>% top_n(10, tot_Unit_Sold))\n", "topItemsEachYear = merge(topItemsEachYear,itemsData, by = c(\"item_nbr\"))\n", "topItemsEachYear2013 = topItemsEachYear\n", "rm(topItemsEachYear)\n", "topItemsEachYear2013"], "metadata": {"_cell_guid": "f4e04363-5b24-4856-85ba-abfc2d78e4a8", "_uuid": "8d8583af9bf4e65761d4515308d1f414aa795bca", "scrolled": true}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***Ploting top 10 best selling items for YEAR 2013 by total unit sale and product family ***"], "metadata": {"_cell_guid": "f88aab4b-09a3-453a-9dea-163f71c3a7a0", "_uuid": "19e71b859c3b4543be6a5943470576a90893114c"}, "cell_type": "markdown"}, {"source": ["ggplot(topItemsEachYear2013, aes(x = item_nbr , y = tot_Unit_Sold, fill = family)) + \n", "geom_bar(stat = \"identity\",width = .7) +  \n", "facet_grid(. ~ year, space=\"free_x\", scales=\"free_x\", labeller=label_both)"], "metadata": {"_cell_guid": "111c0e8c-1d56-465f-b15a-2097f59bd426", "_uuid": "454274ae870881bf26083c784900246891281923", "scrolled": true}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***FOR YEAR 2014***"], "metadata": {"_cell_guid": "1a4646df-f637-4f6b-b23a-40c528086262", "_uuid": "5b93003036339bfd6f02077b61487fcbe0d071ce"}, "cell_type": "markdown"}, {"source": ["trainData_2014 <- merge(trainData_2014,itemsData, by = c(\"item_nbr\"))\n", "trainData_2014$year <- year(as.POSIXlt(trainData_2014$date, format=\"%Y-%m-%d\"))\n", "trainData_2014$item_nbr<- as.factor(trainData_2014$item_nbr)\n", "trainData_2014$year <- as.factor(trainData_2014$year)"], "metadata": {"_cell_guid": "5334ec82-8ca0-4c9b-98a5-196125a470e5", "_uuid": "3bed5596598a19c4bdc30ec6dccee1ba069d2cf4"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***10 Top items In YEAR 2014 by total unit sales***"], "metadata": {"_cell_guid": "4c697ea7-43f3-4d07-8cb7-b74feb99e38b", "_uuid": "5449a0e4c802fd0c6c993bc9286f20ed66634daa"}, "cell_type": "markdown"}, {"source": ["x = data.frame(trainData_2014 %>% group_by(item_nbr,year) %>% summarise(tot_Unit_Sold = sum(unit_sales)))\n", "group <- as.data.table(x)\n", "topItemsEachYear = data.frame(group %>% group_by(year) %>% top_n(10, tot_Unit_Sold))\n", "topItemsEachYear = merge(topItemsEachYear,itemsData, by = c(\"item_nbr\"))\n", "topItemsEachYear2014 = topItemsEachYear\n", "rm(topItemsEachYear)\n", "topItemsEachYear2014"], "metadata": {"_cell_guid": "d32b660c-93ce-4d45-b24c-7beefca2152f", "_uuid": "d6fc95020cb47b8335eb3a62082d4b6d223cca8c"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***Ploting top 10 best selling items for YEAR 2014 by total unit sale and product family ***"], "metadata": {"_cell_guid": "55288ead-ba87-4f68-99fe-a25b46b7394e", "_uuid": "9d61c8cb48377541002826bbb39d87032a34ba6b"}, "cell_type": "markdown"}, {"source": ["ggplot(topItemsEachYear2014, aes(x = item_nbr , y = tot_Unit_Sold, fill = family)) + \n", "geom_bar(stat = \"identity\",width = .7) +  \n", "facet_grid(. ~ year, space=\"free_x\", scales=\"free_x\", labeller=label_both)"], "metadata": {"_cell_guid": "2284bd0d-92ec-44f4-a130-5af01f8872c0", "_uuid": "7a9bcd9e78c6057f409fee33fc88afb018e81a05"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***FOR YEAR 2015***"], "metadata": {"_cell_guid": "c0ca2a00-f943-4e7a-8b3d-9346242934eb", "_uuid": "cf21d2604fbc04e85a88712f7498715d19aebcb6"}, "cell_type": "markdown"}, {"source": ["trainData_2015 <- merge(trainData_2015,itemsData, by = c(\"item_nbr\"))\n", "trainData_2015$year <- year(as.POSIXlt(trainData_2015$date, format=\"%Y-%m-%d\"))\n", "trainData_2015$item_nbr<- as.factor(trainData_2015$item_nbr)\n", "trainData_2015$year <- as.factor(trainData_2015$year)"], "metadata": {"_cell_guid": "42fb9532-25a2-4b00-8bcb-0065097087f9", "_uuid": "75ca1422cc008139c9b7240f768c5558feae4b9b"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***10 Top items In YEAR 2015 by total unit sales***"], "metadata": {"_cell_guid": "f5364778-046f-4b67-99eb-3956388afcdb", "_uuid": "824a1bc5c835009484a858d0fee72a51796097a8"}, "cell_type": "markdown"}, {"source": ["x = data.frame(trainData_2015 %>% group_by(item_nbr,year) %>% summarise(tot_Unit_Sold = sum(unit_sales)))\n", "group <- as.data.table(x)\n", "topItemsEachYear = data.frame(group %>% group_by(year) %>% top_n(10, tot_Unit_Sold))\n", "topItemsEachYear = merge(topItemsEachYear,itemsData, by = c(\"item_nbr\"))\n", "topItemsEachYear2015 = topItemsEachYear\n", "rm(topItemsEachYear)\n", "topItemsEachYear2015"], "metadata": {"_cell_guid": "2fff8730-7288-4b1f-82a0-6528cdf65bdb", "_uuid": "9e84597606c478f5be52274f82ea9f35621781bd"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***Ploting top 10 best selling items for YEAR 2015 by total unit sale and product family ***"], "metadata": {"_cell_guid": "0b562ccb-3b1c-46dc-90a5-7fd1fc53f7c8", "_uuid": "d99f821dac82e35c5673c4311cec038b1014a450"}, "cell_type": "markdown"}, {"source": ["ggplot(topItemsEachYear2015, aes(x = item_nbr , y = tot_Unit_Sold, fill = family)) + \n", "geom_bar(stat = \"identity\",width = .7) +  \n", "facet_grid(. ~ year, space=\"free_x\", scales=\"free_x\", labeller=label_both)"], "metadata": {"_cell_guid": "b0413ccb-d720-4c22-a22e-658cd43cfe85", "_uuid": "113765037dd1f9e0625f52a2af75f7f2747ee340"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***FOR YEAR 2016***"], "metadata": {"_cell_guid": "02a1d52d-036d-4fc7-9b14-db407a31cdb5", "_uuid": "363fa027e00ab823265af3e71c31267a06a9cd75"}, "cell_type": "markdown"}, {"source": ["trainData_2016 <- merge(trainData_2016,itemsData, by = c(\"item_nbr\"))\n", "trainData_2016$year <- year(as.POSIXlt(trainData_2016$date, format=\"%Y-%m-%d\"))\n", "trainData_2016$item_nbr<- as.factor(trainData_2016$item_nbr)\n", "trainData_2016$year <- as.factor(trainData_2016$year)"], "metadata": {"_cell_guid": "4cd39418-644c-407e-9754-a60863e99016", "_uuid": "8c543f2599889387b7cc44e2c22219bc8662dfea"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***10 Top items In YEAR 2016 by total unit sales***"], "metadata": {"_cell_guid": "a8f3197a-375a-4a63-a239-0a58d9873039", "_uuid": "e65870c6762fae37d839700583243526abcb393d"}, "cell_type": "markdown"}, {"source": ["x = data.frame(trainData_2016 %>% group_by(item_nbr,year) %>% summarise(tot_Unit_Sold = sum(unit_sales)))\n", "group <- as.data.table(x)\n", "topItemsEachYear = data.frame(group %>% group_by(year) %>% top_n(10, tot_Unit_Sold))\n", "topItemsEachYear = merge(topItemsEachYear,itemsData, by = c(\"item_nbr\"))\n", "topItemsEachYear2016 = topItemsEachYear\n", "rm(topItemsEachYear)\n", "topItemsEachYear2016"], "metadata": {"_cell_guid": "3cef3cc3-658f-4edc-b947-1f51eafc6949", "_uuid": "a24072198968fba689ca7629c53079e996f55870"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***Ploting top 10 best selling items for YEAR 2016 by total unit sale and product family ***"], "metadata": {"_cell_guid": "aa77800c-f270-4623-a08c-ab3138460226", "_uuid": "6e97697def1e04004a23e9287a755b41d6c8d75b"}, "cell_type": "markdown"}, {"source": ["ggplot(topItemsEachYear2016, aes(x = item_nbr , y = tot_Unit_Sold, fill = family)) + \n", "geom_bar(stat = \"identity\",width = .7) +  \n", "facet_grid(. ~ year, space=\"free_x\", scales=\"free_x\", labeller=label_both)"], "metadata": {"_cell_guid": "4365b2ae-8cb6-4227-adec-b4fae3872cc3", "_uuid": "4e535a3c5bfe52c47f81de658892b4836c80b3dc"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["**FOR YEAR 2017****\n"], "metadata": {"_cell_guid": "b1a10f4f-131d-4cdc-b269-c1f46ef3f4fe", "_uuid": "594115652d75f9cb5ce2f0085524546d0740ef74"}, "cell_type": "markdown"}, {"source": ["trainData_2017 <- merge(trainData_2017,itemsData, by = c(\"item_nbr\"))\n", "trainData_2017$year <- year(as.POSIXlt(trainData_2017$date, format=\"%Y-%m-%d\"))\n", "trainData_2017$item_nbr<- as.factor(trainData_2017$item_nbr)\n", "trainData_2017$year <- as.factor(trainData_2017$year)"], "metadata": {"_cell_guid": "25c2db05-4f5a-49eb-8b0a-72a0f0511e8c", "_uuid": "913d56d176d11ae1311bb21b4fd1bb74dc493d91"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["***10 Top items In YEAR 2017 by total unit sales***"], "metadata": {"_cell_guid": "7f2941cd-0067-4910-baaa-dde7c26ba8be", "_uuid": "f34225c58b835e68701b1504a648bc5115d24cdc"}, "cell_type": "markdown"}, {"source": ["x = data.frame(trainData_2017 %>% group_by(item_nbr,year) %>% summarise(tot_Unit_Sold = sum(unit_sales)))\n", "group <- as.data.table(x)\n", "topItemsEachYear = data.frame(group %>% group_by(year) %>% top_n(10, tot_Unit_Sold))\n", "topItemsEachYear = merge(topItemsEachYear,itemsData, by = c(\"item_nbr\"))\n", "topItemsEachYear2017 = topItemsEachYear\n", "rm(topItemsEachYear)\n", "topItemsEachYear2017"], "metadata": {"_cell_guid": "1923c6a1-6c2a-436d-b32c-b7bf997eb242", "_uuid": "13d0c903441a5beab62f935e18edc97b53e0c8fe"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["**Ploting top 10 best selling items for YEAR 2017 by total unit sale and product family****"], "metadata": {"_cell_guid": "e16b610a-6d3b-4c1f-9950-10be8adc2156", "_uuid": "b5827b54883390c714c8056aa8586933f751bffe"}, "cell_type": "markdown"}, {"source": ["ggplot(topItemsEachYear2017, aes(x = item_nbr , y = tot_Unit_Sold, fill = family)) + \n", "geom_bar(stat = \"identity\",width = .7) +  \n", "facet_grid(. ~ year, space=\"free_x\", scales=\"free_x\", labeller=label_both)"], "metadata": {"_cell_guid": "3b43e3fe-4fbd-42a3-9546-5718a5faea29", "_uuid": "9b127daabf4f065a0aeecfb7ba424d801293f17a"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["Combining all dataframes togeather and ploting them togeather"], "metadata": {"_cell_guid": "972874ff-c9d6-46c5-aea5-6c49dbcd7c1f", "_uuid": "b250bea19cd4523fefaf261902762e85ea5106b6"}, "cell_type": "markdown"}, {"source": ["dfList = c('topItemsEachYear2013','topItemsEachYear2014', 'topItemsEachYear2015', 'topItemsEachYear2016', 'topItemsEachYear2017')\n", "x.list <- lapply(dfList, get) \n", "do.call(rbind, x.list)"], "metadata": {"_cell_guid": "0675737b-47d8-45cd-b043-6a737170e12e", "_uuid": "fbff09abf5e0696cc8210edce075a67907220726"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["\n", "ggplot(do.call(rbind, x.list), aes(x = item_nbr , y = tot_Unit_Sold, fill = family)) + \n", "geom_bar(stat = \"identity\",width = .7) +  \n", "facet_grid(. ~ year, space=\"free_x\", scales=\"free_x\", labeller=label_both)"], "metadata": {"_cell_guid": "cd7839d7-de69-4fcf-850b-1a016ebcddf4", "_uuid": "3463a16ae1482e5e686b354e24086297c7750017"}, "outputs": [], "execution_count": null, "cell_type": "code"}, {"source": ["**Conclusion :  From the above process below are few of the highlights about the top selling items**\n", "\n", "a )  ***Top seller of year 2014, 2015,2016,2017*** ==>   ***Item # 1503844***           (Family Produce )\n", "\n", "b)  ***Best sellers in year 2013, 2014,2015,2016,2017 ***  ==>  ***Item # 1473474*** ( Family Beverage) \n", "\n", "c) ***Best Seller of years 2014,2015,2016,2017*** ==> ***Item # 1473474***              ( Family Produce ) \n", "\n", "d) Products from family Beverage are the most selling items in all years with      \n", "     \n", "     4 products in year 2013, \n", "     5 products in year 2014,2015 and 2016 and \n", "     7 products in year 2017\n", "  "], "metadata": {"_cell_guid": "e05d4b46-9113-4d59-8a2f-513016e5c19e", "_uuid": "2629fc1f2de6af10bfc21ec345a64559f76f5a83"}, "cell_type": "markdown"}]}