# 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)
library(knitr)
library(dplyr)

# 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")

# Any results you write to the current directory are saved as output.
train = read_csv("../input/train.csv")

ggplot(filter(count(train, Demanda_uni_equil), Demanda_uni_equil<50), aes(x = Demanda_uni_equil, y = n))+
    geom_bar(stat = "identity")+
    ggtitle("Demand Histogram")
    
#sapply(train, function(x)length(unique(x)))

#product_count = count(train, Producto_ID, sort = TRUE)
#top_products = product_count$Producto_ID[1:12]
#print("product_count")
#product_count
#print("top_products")
#top_products

#ggplot(filter(filter(train, Producto_ID %in% top_products), Demanda_uni_equil < 20), aes(x = Demanda_uni_equil, fill = Producto_ID))+
#   geom_bar()+
#    facet_wrap( ~ Producto_ID) +
#    ggtitle("Demand Distributions for top 20 Products")