Introduction

library(dplyr)
library(ggplot2)
library(readr)


# Extra Libraries
library(tidyr)
library(reshape2)
#library(data.table)
library(DT)
library(d3heatmap)
library(lubridate)
#library(prophet)  # Facebook's new package
library(xts)
library(dygraphs)
library(forecast)
library(corrplot)



# Functions

wd<-function(x) as.POSIXct(strptime(x, '%m/%d/%Y %I:%M:00 %p'))
byMonth <- function(x) strftime(x, '%Y-%m-01')
backDate <- function(x) as.POSIXct(strptime(x, '%Y-%m-%d'))
monthName <- function(x) strftime(x, '%b')

weekDayName <- function(x) strftime(x,'%a')
hourName <- function(x) strftime(x,'%H')


# Reading Input
d <- read.csv("../input/crime.csv", sep=",")

# Apply Functions

d$timeStamp <- wd(d$incident_datetime)
d$month <- byMonth(d$timeStamp)
d$Month <- cut(d$timeStamp, breaks= "month")
d$Year <- cut(d$timeStamp, breaks= "year")
d$Week <- cut(d$timeStamp, breaks= "week")
d$Quarter <- cut(d$timeStamp, breaks= "quarter")
d$monthName <- monthName(d$timeStamp)

d$day  = weekDayName(d$timeStamp)
d$hour = hourName(d$timeStamp)

# Handy for Year-to-date
d$monthN <-  month(d$timeStamp)


# There are some bad dates in this...
d <- d[d$timeStamp > "2010-01-01 00:00:00",]


# Don't want NA when grouping
d[is.na(d)] <- 0
this_day   <- today()
this_month <- month(this_day)
this_year  <- year(this_day)
t = d[d$monthN < this_month,]

counts <- summarise(group_by(t, parent_incident_type,Year), Counts=length(parent_incident_type))
counts <- counts[order(counts$Year),]


colnames(counts) <- c("parent_incident_type", 
                      "YTD","Counts")


#p = dcast(counts, YTD ~ parent_incident_type)
p = dcast(counts, YTD ~ parent_incident_type, value.var="Counts")
p$Var.2 <- NULL  # Not sure what this is, but take him out.
p$YTD  <- year(p$YTD)

# Safety check
p[is.na(p)] <- 0

# Only crimes that are increasing from last YTD
#  
tmp <- p[p$YTD == (this_year),]  - p[p$YTD == (this_year - 1),] 
p <- p[ , colSums(tmp) > 0]

# Make it known this is only Jan/Feb
p$YTD=sprintf("%d %s",p$YTD,"Jan+Feb")



# Make YTD the row name
row.names(p) <- p$YTD

# Remove first
p = p[,-1]
p <- p[ rev(row.names(p)), ]

# Save the category
colsNames_increasing <- colnames(p)



myTable <- datatable(p, extensions = 'FixedColumns', options = list(
    dom = 't',
    deferRender = TRUE,
    scrollX = TRUE,
    scroller = TRUE,
    fixedColumns = list(leftColumns = 1, rightColumns = 0)))


# Summarize for “Theft from Vehicle”   
#cd=summarise(group_by(d[grep("Theft from Veh", d$parent_incident_type), ], dsym), y=length(dsym))
#cd

Year-to-Date Values: Only Crimes Increasing from Last Year

Cheltenham High School

Year to date… only increases

this_day   <- today()
this_month <- month(this_day)
this_year  <- year(this_day)

t <- d[grep("500 Block RICES MILL RD", d$address_1), ]
#t = t[t$year < this_year,]


counts <- summarise(group_by(t, parent_incident_type,Year), Counts=length(parent_incident_type))
counts <- counts[order(counts$Year),]


colnames(counts) <- c("parent_incident_type", 
                      "YTD","Counts")


p = dcast(counts, YTD ~ parent_incident_type, value.var="Counts")

p$Var.2 <- NULL  # Not sure what this is, but take him out.
p$YTD  <- year(p$YTD)

# Safety check
p[is.na(p)] <- 0

# Only crimes that are increasing from last YTD
#  
#  tmp <- p[p$YTD == (this_year),]  - p[p$YTD == (this_year - 1),] 
#  p <- p[ , colSums(tmp) > 0]


# Make YTD the row name
row.names(p) <- p$YTD

# Remove first
p = p[,-1]
p <- p[ rev(row.names(p)), ]

# Save the category
colsNames_increasing <- colnames(p)

myTable <- datatable(p, extensions = 'FixedColumns', options = list(
    dom = 't',
    deferRender = TRUE,
    scrollX = TRUE,
    scroller = TRUE,
    fixedColumns = list(leftColumns = 1, rightColumns = 0)))

Cheltenham High School - Yearly

myTable