#################
## Set up Cluster (H2O is a Java ML Platform, with R/Python/Web/Java APIs)
#################
print(paste("Set up Cluster",Sys.time()))
library(h2o) # R API is just a library
## start a cluster; default is 2 cores for CRAN reasons; -1 = all
h2o.init(nthreads=-1,max_mem_size='7G')  
#############
## Load Data 
#############
print(paste("Load Data",Sys.time()))
## load the full training file, using all cores; load into H2O Frame known to R as train; H2O as train.hex
train<-h2o.uploadFile("../input/train.csv",destination_frame = "train.hex")
print(paste("Loaded",Sys.time()))

train[1:2,] ## take a look at a few rows

groups<-h2o.group_by(data=train,by=c("Semana","Canal_ID"),sum("Venta_hoy"))
groups[1:5,]

towns<-h2o.uploadFile("../input/town_state.csv",destination_frame = "towns.hex")

sales<-h2o.group_by(data=train,by="Agencia_ID",sum("Venta_hoy"))
sales[1:5,]

agencia_sales<-h2o.merge(towns, sales, all.x = FALSE, all.y = FALSE)
agencia_sales[1:5,]