Day with most Homicides


In [14]:
library(httr)

# Pull the NewsroomDB URL from an environment variable so we don't expose our super-secret data feed ;-)
kNewsroomDbUrl <- Sys.getenv("NEWSROOMDB_URL")

# Download homicides CSV

r <- GET(paste0(kNewsroomDbUrl, "table/csv/homicides"))

# Load CSV into a dataframe
homicide.victims.csv <- content(r, "text")
con <- textConnection(homicide.victims.csv)
homicide.victims <- read.csv(con)
close(con)

# Parse some of the columns

# Convert the Date column to a date object
homicide.victims$Occ.Date <- as.Date(homicide.victims$Occ.Date, format="%Y-%m-%d")

# Extract the year from the date
homicide.victims$Year <- as.numeric(format(homicide.victims$Occ.Date,'%Y'))

In [15]:
library(dplyr)

# Get only 2016 homicide victims
homicide.victims.2016 <- filter(homicide.victims, Year == 2016)

# Group them by day
homicide.victims.2016.by.date <- group_by(homicide.victims.2016, Occ.Date)

# Count the homicide victims each day and sort in descending order
homicide.victims.2016.date.counts <- summarise(homicide.victims.2016.by.date, count = n()) %>%
  arrange(desc(count))

In [16]:
homicide.victims.2016.date.counts


Occ.Datecount
2016-08-089
2016-09-059
2016-02-028
2016-08-238
2016-06-186
2016-08-146
2016-08-216
2016-01-255
2016-05-095
2016-05-155
2016-06-045
2016-06-165
2016-07-175
2016-07-285
2016-08-165
2016-08-285
2016-09-115
2016-09-185
2016-01-054
2016-01-084
2016-01-234
2016-01-314
2016-02-044
2016-02-134
2016-03-114
2016-03-154
2016-03-314
2016-04-074
2016-04-244
2016-04-304
2016-05-291
2016-06-021
2016-06-151
2016-06-221
2016-06-281
2016-06-301
2016-07-031
2016-07-091
2016-07-101
2016-07-151
2016-07-191
2016-07-231
2016-07-261
2016-08-011
2016-08-151
2016-08-171
2016-08-221
2016-09-011
2016-09-021
2016-09-071
2016-09-081
2016-09-091
2016-09-121
2016-09-141
2016-09-151
2016-09-191
2016-09-201
2016-09-221
2016-09-251
2016-09-271

In [ ]: