{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages\n# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats\n# For example, here's several helpful packages to load in \n\nlibrary(ggplot2) # Data visualization\nlibrary(readr) # CSV file I/O, e.g. the read_csv function\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nsystem(\"ls ../input\")\n\n# Any results you write to the current directory are saved as output.\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Loading useful libraries\n\nlibrary(readr)\nlibrary(sqldf)\nlibrary(ggplot2)\n\n# setting the evaluation functions for optimization\n\nrmsle <- function(predicted, actual) \n         {sqrt(mean((log(predicted+1)-log(actual+1))^2))\n}\n\n# using read_csv to quickly read all datasets\n\n# reading primary datasets: training, testing and submission\n\ntrain <- read_csv(\"../input/train.csv\",col_names = TRUE)\ntest <- read_csv(\"../input/test.csv\",col_names = TRUE)\nsubmission <- read_csv(\"../input/sample_submission.csv\",col_names = TRUE)\n\n# reading secondary datasets: cliente_tabla, town_state and producto_tabla\n\nclient <- read_csv(\"../input/cliente_tabla.csv\",col_names = TRUE)\nproduct <- read_csv(\"../input/producto_tabla.csv\",col_names = TRUE)\ntown_state <- read_csv(\"../input/town_state.csv\",col_names = TRUE)\n\n# cleaning the column names from spanish to english for better understanding of content\n\n# cleaning for training dataset\n\nnames(train)<-c(\"week_num\",\"sales_depot_id\",\"sales_channel_id\",\"route_id\",\"client_id\",\"product_id\",\"sales_unit\",\"sales\",\"return_unit\",\"return\",\"adj_demand\")\n\n# cleaning for test dataset\n\nnames(test)<-c(\"id\",\"week_num\",\"sales_depot_id\",\"sales_channel_id\",\"route_id\",\"client_id\",\"product_id\")\n\n# cleaning for client dataset\n\nnames(client)<-c(\"client_id\",\"clientname\") # language change for column name\nclient<-sqldf(\"select * from client group by client_id\") # grouping duplicated client records\n\ncname<-as.factor(client$clientname)\nstr(cname) \n# side discovery that many clients are having the same name recorded\n# For example, we can see that client_id 4110 and 5352 and so on are having same name \"LOS ANGELES\"\n\n###\n\n# cleaning for product dataset\n\nnames(product)<-c(\"product_id\",\"productname\") # language change for column name\nproduct<-sqldf(\"select * from product group by product_id\") # grouping duplicated product records\n\n# cleaning for town_state dataset\n\nnames(town_state)<-c(\"sales_depot_id\",\"town\",\"state\") # language change for column name\ntown_state<-sqldf(\"select * from town_state group by sales_depot_id\") # grouping duplicated product records\n\n# viewing head and tail of each dataset\n\nhead(trian,5)\ntail(train,5)\n\nhead(test,5)\ntail(test,5)\n\nhead(client,5)\ntail(client,5)\n\nhead(product,5)\ntail(product,5)\n\nhead(town_state,5)\ntail(town_state,5)"
 }
],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"}}, "nbformat": 4, "nbformat_minor": 0}