Working with dates

One of the most frustrating things in data is working with dates. Everyone has a different opinion on how to record them, and every software package on the planet has to sort it out. Dealing with it can be a little ... confusing. And every dataset has something new to throw at you. So consider this an introduction.

We're going to do this two ways. First we're going to use base R to solve a tricky problem. And then we'll use a library called lubridate to solve a more common and less tricky problem.

First, we'll import dyplyr like we always do.


In [1]:
library(dplyr)


Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

Next we'll use the UNL crime data we used earlier. This time, we want to see how many incidents are reported every year to UNLPD and how much that has changed.


In [2]:
unlcrime <- read.csv("../../Data/unlcrime.csv", stringsAsFactors = FALSE)

In [3]:
head(unlcrime)


CaseNoIncident.CodeReportedCase.StatusStart.OccurredEnd.OccurredBuildingLocationStolenDamagedDescription
11000001 NARCOTICS - POSSESSION 2011-01-01T00:10:00 Cleared by Arrest 2011-01-01T00:10:00 515 N 19th Parking Lot Vine to 515 North 19th St. (GOV'T PARKING LOT (UNL)) NA NA UNL student cited and released for Poss of Marijuana < 1 oz and Poss of Drug Paraphernalia after a CSO reported suspicious drug-related activity from a vehicle. 1.2 g of marijuana seized.
11000011 NARCOTICS - POSSESSION 2011-01-01T23:16:00 Cleared by Arrest 2011-01-01T23:16:00 N 10TH ST & CHARLESTON ST (STREET) NA NA Traffic Stop. Vehicle stopped for a headlight out. Upon contact Officer smelled the strong odor of burnt marijuana. Driver cited/released for Possession of Drug Paraphernalia after a search yielded a pipe with burnt marijuana residue.
11000013 ALCOHOL - MINOR IN POSSESSION 2011-01-02T00:38:00 Cleared by Arrest 2011-01-02T00:38:00 N 17TH ST & N ANTELOPE VALLEY PKWY (STREET) NA NA Three persons cited for MIPC after being stopped for no headlights at N. Antelope valley parkway and N. 17rh Street.
11000019 TRAFFIC - SUSPENDED DRIVER 2011-01-02T23:24:00 Cleared by Arrest 2011-01-02T23:24:00 N 17TH ST & VINE ST (STREET) NA NA Non-student cited and lodged in jail for Driving Under Suspension after being stopped for speeding.
11000020 NARCOTICS - POSSESSION 2011-01-03T04:46:00 Cleared by Arrest 2011-01-03T04:46:00 N 10TH ST & CHARLESTON ST (STREET) NA NA Non-student stopped for speeding. Officer smelled burnt marijuana from within the vehicle and upon searching located burnt marijuana cigarettes, a pipe, and .6 g of marijuana. Cited and released for Speeding, Poss of Marijuana < 1 oz, and Poss of Drug Paraphernalia.
11000022 MEDICAL EMERGENCY 2011-01-03T10:34:00 Inactive 2011-01-03T10:34:00 Campus Recreation Center 841 North 14th St (SPORT FACILITIES) NA NA UNL affiliated female disoriented in need of medical attention at the Rec Center. Transported to the hospital by LFR.

Note we added stringsAsFactors = FALSE to the party. Why? Because if we don't, then R tries to import dates as numbers, which makes parsing them into actual dates very, very hard. So this solves some problems for us.

The code to fix dates is a little convoluted, but once you learn the pattern, it's not so bad. We're going to do this in steps.

First, let's just mutate our data to create a new field with properly formatted dates. Here's what that looks like:


In [4]:
unlcrime %>% mutate(
    CleanDate = as.POSIXct(Reported, format="%Y-%m-%dT%H:%M"),
)


CaseNoIncident.CodeReportedCase.StatusStart.OccurredEnd.OccurredBuildingLocationStolenDamagedDescriptionCleanDate
11000001 NARCOTICS - POSSESSION 2011-01-01T00:10:00 Cleared by Arrest 2011-01-01T00:10:00 515 N 19th Parking Lot Vine to 515 North 19th St. (GOV'T PARKING LOT (UNL)) NA NA UNL student cited and released for Poss of Marijuana < 1 oz and Poss of Drug Paraphernalia after a CSO reported suspicious drug-related activity from a vehicle. 1.2 g of marijuana seized. 2011-01-01 00:10:00
11000011 NARCOTICS - POSSESSION 2011-01-01T23:16:00 Cleared by Arrest 2011-01-01T23:16:00 N 10TH ST & CHARLESTON ST (STREET) NA NA Traffic Stop. Vehicle stopped for a headlight out. Upon contact Officer smelled the strong odor of burnt marijuana. Driver cited/released for Possession of Drug Paraphernalia after a search yielded a pipe with burnt marijuana residue. 2011-01-01 23:16:00
11000013 ALCOHOL - MINOR IN POSSESSION 2011-01-02T00:38:00 Cleared by Arrest 2011-01-02T00:38:00 N 17TH ST & N ANTELOPE VALLEY PKWY (STREET) NA NA Three persons cited for MIPC after being stopped for no headlights at N. Antelope valley parkway and N. 17rh Street. 2011-01-02 00:38:00
11000019 TRAFFIC - SUSPENDED DRIVER 2011-01-02T23:24:00 Cleared by Arrest 2011-01-02T23:24:00 N 17TH ST & VINE ST (STREET) NA NA Non-student cited and lodged in jail for Driving Under Suspension after being stopped for speeding. 2011-01-02 23:24:00
11000020 NARCOTICS - POSSESSION 2011-01-03T04:46:00 Cleared by Arrest 2011-01-03T04:46:00 N 10TH ST & CHARLESTON ST (STREET) NA NA Non-student stopped for speeding. Officer smelled burnt marijuana from within the vehicle and upon searching located burnt marijuana cigarettes, a pipe, and .6 g of marijuana. Cited and released for Speeding, Poss of Marijuana < 1 oz, and Poss of Drug Paraphernalia.2011-01-03 04:46:00
11000022 MEDICAL EMERGENCY 2011-01-03T10:34:00 Inactive 2011-01-03T10:34:00 Campus Recreation Center 841 North 14th St (SPORT FACILITIES) NA NA UNL affiliated female disoriented in need of medical attention at the Rec Center. Transported to the hospital by LFR. 2011-01-03 10:34:00
11000024 FRAUD - CREDIT CARDS/ATM/BANK CARD 2011-01-03T14:25:00 Inactive 2010-12-21T00:00:00 Cyberspace/Internet (INTERNET/CYBERSPACE) NA NA Person received items they did not order on their credit card, unknown who may have used their card. 2011-01-03 14:25:00
11000027 FIRE (WORKING) - ALARM 2011-01-03T20:51:00 Cleared by Exception 2011-01-03T20:51:00 Memorial Stadium 740 Stadium Dr. (SPORT FACILITIES) NA NA Fire alarm at Memorial Stadium caused by broken water pipe, no damage at this time, LFR on scene. 2011-01-03 20:51:00
11000030 LARCENY - MOTOR VEH. ACCESSORIES 2011-01-04T10:57:00 Inactive 2010-12-03T17:00:00 2011-01-04T10:57:00 Hardin Hall 1600 North 33rd St (GOV'T PARKING LOT (UNL)) 400 500 Theft of a catalytic converter from a state owned vehicle. 2011-01-04 10:57:00
11000032 DISTURBANCE - OTHER 2011-01-04T16:40:00 Inactive Burnett Hall 1220 T St (UNL ACADEMIC BUILDING) NA NA Person received a letter from another person which disturbed them. 2011-01-04 16:40:00
11000033 PROPERTY DAMAGE - UNINTENTIONAL,NON TRAFFIC 2011-01-04T17:18:00 Cleared by Exception 2011-01-04T08:00:00 2011-01-04T17:00:00 Stadium Loop Parking Lot (GOV'T PARKING LOT (UNL)) NA NA Mirror had paint traces on it from another vehicle touching the mirror while backing out of stall, no damage. This occured in the area 10 Loop 14th and Vine Street. 2011-01-04 17:18:00
11000036 TRESPASSING 2011-01-04T21:43:00 Cleared by Exception 2011-01-04T21:43:00 Oldfather Hall 660 North 12th St (UNL ACADEMIC BUILDING) NA NA Non-student banned from University buildings after viewing pornography on computers. 2011-01-04 21:43:00
11000044 DISTURBANCE - OTHER 2011-01-06T08:43:00 Inactive 2011-01-03T12:53:00 (INTERNET/CYBERSPACE) NA NA UNL staff reported receiving unwanted emails. 2011-01-06 08:43:00
11000048 FIRE (WORKING) - ALARM 2011-01-06T17:18:00 Cleared by Exception 2011-01-06T17:18:00 2244 Vine Street 2244 Vine Street (UNL APARTMENT) NA 15 LFR responded to a fire on the balcony of building, 3rd floor. Later found resident had been smoking on the balcony and put a cigarette butt in flower pot causing it's contents to start on fire. Damage to flower pot, no damage to structure. 2011-01-06 17:18:00
11000049 OUTSIDE - O.P.S. OTHER 2011-01-06T17:44:00 Inactive 2011-01-06T16:00:00 2011-01-06T17:44:00 3201 W Street (SIDEWALK) 300 NA Non UNL affiliated person had $300.00 stolen. This occurred off campus and was tunred over to LPD. 2011-01-06 17:44:00
11000058 TRESPASSING 2011-01-07T10:00:00 Cleared by Exception 2011-01-07T10:00:00 Love Library South 1248 R (LIBRARY) NA NA Transient taken to detox after he passed out in Love Library (PBT refused). He was issued a Ban and Bar Letter. 2011-01-07 10:00:00
11000061 PROPERTY DAMAGE - UNINTENTIONAL,NON TRAFFIC 2011-01-07T14:17:00 Cleared by Exception 2011-01-07T14:17:00 Lied Center 301 North 12th St (UNL SUPPORT FACILITY (office, service)) NA 350 Wind blew a dumpster into a vehicle owned by UNL ($350 damage). 2011-01-07 14:17:00
11000063 TRAFFIC - OTHER 2011-01-08T02:50:00 Cleared by Arrest 2011-01-08T02:50:00 N 17TH ST & R ST (STREET) NA NA UNL student stopped for expired license plates and found to have consumed alcohol. Ultimately tested under legal limit. Taken to Detox and cited for Reckless Driving. .072 BAC 2011-01-08 02:50:00
11000065 LARCENY - STOLEN BIKE 2011-01-08T10:40:00 Inactive Westbrook Music Building 1104 R St (UNL ACADEMIC BUILDING) 50 NA Black and green Gary Fischer bicycle stolen sometime in September 2010. 2011-01-08 10:40:00
11000068 MEDICAL EMERGENCY 2011-01-08T14:46:00 Cleared by Exception 2011-01-08T14:46:00 Bob Devaney Sports Center 1600 Court St (SPORT FACILITIES) NA NA Non UNL affiliated male suffered a seizure while attending a basketball game. 2011-01-08 14:46:00
11000070 OUTSIDE - O.P.S. OTHER 2011-01-08T19:35:00 Cleared by Exception 2011-01-03T08:00:00 2011-01-08T19:30:00 1525 South 19th #5 (DUPLEX ___���� off campus) NA NA UNL student reported being concerned about the behavior of an ex-boyfriend. 2011-01-08 19:35:00
11000071 ALCOHOL - DWI 2011-01-09T02:21:00 Cleared by Arrest 2011-01-09T02:21:00 X ST & N 16TH ST (STREET) NA NA 4 female UNL students contacted after a traffic infraction. 1 cited for DUI, 3 cited for MIPC, open container and false information. BAC. for driver .181. 2011-01-09 02:21:00
11000072 ALCOHOL - MINOR IN POSSESSION 2011-01-09T03:05:00 Cleared by Arrest 2011-01-09T03:05:00 Robert E. Knoll Residence Center 440 N 17th Street (UNL RESIDENCE HALL) NA NA CSO reported possible alcohol activity. Four UNL students cited and released for MIP after alcohol was observed in the room. .089, .049, .055, & .176 BAC's 2011-01-09 03:05:00
11000073 NARCOTICS - POSSESSION 2011-01-09T10:20:00 Inactive 2011-01-07T18:00:00 Robert E. Knoll Residence Center 440 N 17th Street (UNL RESIDENCE HALL) NA NA Marijuana pipe found under a couch cushion in the room while new residents were moving in. 2011-01-09 10:20:00
11000076 PROPERTY DAMAGE - UNINTENTIONAL,NON TRAFFIC 2011-01-10T08:37:00 Cleared by Exception 2011-01-10T08:37:00 College of Dentistry 3975 Potter St (GOV'T PARKING LOT (UNL)) NA 800 UNL owned snow blower struck a parked vehicle. 2011-01-10 08:37:00
11000078 PROPERTY DAMAGE - UNINTENTIONAL,NON TRAFFIC 2011-01-10T11:10:00 Cleared by Exception 2011-01-10T11:10:00 Pound Hall 513 North 17th St (SIDEWALK) NA 100 UNL snow plow struck a meter while clearing the sidewalk of snow causing it to slide into a parked vehicle. Damage to City of Lincoln meter (Bent over) and parked vehicle (Driver's side mirror). 2011-01-10 11:10:00
11000082 LARCENY - FROM MOTOR VEHICLE 2011-01-10T14:47:00 Inactive 2010-12-15T12:00:00 2011-01-07T16:00:00 Husker Hall 705 North 23rd St (GOV'T PARKING LOT (UNL)) 780 50 Belated report of a laptop computer and Sony digital recorder stolen from vehicle. Pry marks found on passenger side door frame. Occurred between December 15th, 2010 to January 7th, 2011. 2011-01-10 14:47:00
11000083 LOST OR STOLEN ITEM 2011-01-10T16:43:00 Inactive 2011-01-09T13:30:00 2011-01-09T15:00:00 Campus Recreation Center 841 North 14th St (SPORT FACILITIES) 43 NA Black bi-fold wallet and contents taken from an unsecured locker in the men's locker room. Debit/Credit cards were canceled by victim. 2011-01-10 16:43:00
11000085 NARCOTICS - POSSESSION 2011-01-10T23:24:00 Cleared by Arrest 2011-01-10T23:24:00 2011-01-10T23:24:00 Abel Hall 880 North 17th St (UNL RESIDENCE HALL) NA NA Person cited for Possession of Marijuana less than an ounce, this occured in Abel Hall. 2011-01-10 23:24:00
11000086 SS - OTHER 2011-01-11T00:53:00 Inactive 2011-01-11T00:53:00 Selleck Quad 4000 600 North 15th St. (UNL RESIDENCE HALL) NA NA Safe found unlocked in Selleck Quad, contents inventoried and safe locked. 2011-01-11 00:53:00
16005585 ACCIDENTS - P.D. REPORTABLE 2016-12-19T14:04:00 Closed - Inactive 2016-12-19T14:04:00 Business Administration Building 512 North 12th St (GOV'T PARKING LOT (UNL)) 0 100 Two vehicle non injury accident involving a UNL vehicle, dock of Love Library. 2016-12-19 14:04:00
16005588 ACCIDENTS - P.D. NOT REPORTABLE 2016-12-19T21:53:00 Closed - Non-Criminal Case 2016-12-19T21:44:00 440 N 17th Street (GOV'T PARKING LOT (UNL)) 0 550 A Nebraska student reported they had hit a sign with their vehicle. 2016-12-19 21:53:00
16005598 ALCOHOL - DWI 2016-12-20T16:03:00 Closed - Cleared by Arrest-Adult 2016-12-20T16:03:00 N 17TH ST & Q ST (GAS/SERVICE STATION) 0 0 Student contacted at 17th and Q Street, found to be under the influence of drugs. Cited for DUI-D and Refusing a Chemical Test. 2016-12-20 16:03:00
16005599 TRAFFIC - SUSPENDED DRIVER 2016-12-20T20:09:00 Closed - Cleared by Arrest-Adult 2016-12-20T20:09:00 N ANTELOPE VALLEY PKWY & Salt Creek RDWY (STREET) 0 0 Vehicle stopped at 21st and SCR, driver found to be suspend. Driver cited for DUS and Improper turn and released. 2016-12-20 20:09:00
16005607 NARCOTICS - POSSESSION 2016-12-21T16:34:00 Closed - Cleared by Arrest-Adult 2016-12-21T16:34:00 N 17TH ST & Q ST (STREET) 0 0 Vehicle stopped for no plates or paper. Marijuana was found in the vehicle. Driver cited for Possession of Marijuana and Possession of Drug Paraphernalia. 2016-12-21 16:34:00
16005609 TRAFFIC - SUSPENDED DRIVER 2016-12-21T19:14:00 Closed - Cleared by Arrest-Adult 2016-12-21T19:14:00 N ANTELOPE VALLEY PKWY & Salt Creek RDWY (STREET) 0 0 A vehicle was stopped for a traffic violation. A non-affiliate was cited/lodged for DUS and cited for Failure to signal turn. 2016-12-21 19:14:00
16005611 ALCOHOL - MINOR IN POSSESSION 2016-12-21T20:37:00 Closed - Cleared by Arrest-Adult 2016-12-21T20:37:00 University Suites 1780 "R" Street (GOV'T PARKING LOT (UNL)) 0 0 A student and a non-affiliate were contacted in a vehicle due to suspicious activity. Both were cited/released for MIP and the student was also cited/released for Possession of Marijuana < 1 oz. 2016-12-21 20:37:00
16005613 ARSON 2016-12-21T22:02:00 Closed - Cleared by Arrest-Adult 2016-12-21T09:43:00 N ANTELOPE VALLEY PKWY & Salt Creek RDWY (OTHER LOT/FIELD) 0 0 A report of a working fire set intentionally by an unknown person. Investigation continues. 2016-12-21 22:02:00
16005616 NARCOTICS - POSSESSION 2016-12-22T01:38:00 Closed - Cleared by Arrest-Adult 2016-12-22T01:38:00 ANTELOPE VALLEY PARKWAY & Q ST (STREET) 0 0 Officer identified driver to be suspended using MDT. Driver was cited/lodged for Possession of a Controlled Substance. Cited/released for Possession of Substance, DUS, Open Container, Possession of Marijuana < 1oz. and Possession of Drug Paraphernalia. Passenger cited/lodged for Possession of a Controlled Substance. Cited/released for Possession of Marijuana < 1 oz and Possession of Drug Paraphernalia. Both are non-affiliates.2016-12-22 01:38:00
16005617 VANDALISM - OTHER 2016-12-22T05:35:00 Closed - Cleared by Arrest-Adult Nebraska Champions Club 707 Stadium Dr (GOV'T PARKING LOT (UNL)) 0 25 The inner pan of a trash receptacle was found to be damaged. 2016-12-22 05:35:00
16005633 OUTSIDE - O.P.S. OTHER 2016-12-22T07:19:00 Closed - Inactive (INTERNET/CYBERSPACE) 0 0 University of Nebraska reported a server breach, case turned over to FBI. 2016-12-22 07:19:00
16005627 TRAFFIC - SUSPENDED DRIVER 2016-12-22T22:52:00 Closed - Cleared by Arrest-Adult 2016-12-22T22:52:00 ANTELOPE VALLEY PARKWAY & SALT CREEK RD (STREET) 0 0 Non-affiliate was lodged in jail for Driving Under Suspension and cited for narcotic offenses. 2016-12-22 22:52:00
16005629 ASSAULT - DOMESTIC 2016-12-23T00:28:00 Closed - Cleared by Arrest-Adult 2016-12-23T00:28:00 Beadle Center 1901 Vine St (UNL ACADEMIC BUILDING) 0 0 University affiliate was lodged in jail for 3rd Degree Domestic Assault. 2016-12-23 00:28:00
16005630 ALCOHOL - DRUNK 2016-12-23T01:46:00 Closed - Non-Criminal Case 2016-12-23T01:46:00 N 8TH ST & Q ST (SIDEWALK) 0 0 Non affiliate contacted and taken to The Bridge (.214 BAC). 2016-12-23 01:46:00
16005636 TRAFFIC - SUSPENDED DRIVER 2016-12-23T18:34:00 Closed - Cleared by Arrest-Adult 2016-12-23T18:34:00 N ANTELOPE VALLEY PKWY & MILITARY RD (STREET) 0 0 A non-affiliate was contacted due to suspended driving privileges. The driver was cited/released for DUS. 2016-12-23 18:34:00
16005638 ALCOHOL - DWI 2016-12-23T22:25:00 Closed - Cleared by Arrest-Adult 2016-12-23T22:25:00 P ST & N 10TH ST (STREET) 0 0 Non university affiliate was cited for DUI <.15 (.105 BAC) and lodged for Criminal Impersonation. 2016-12-23 22:25:00
16005639 TRAFFIC - SUSPENDED DRIVER 2016-12-23T23:06:00 Closed - Cleared by Arrest-Adult 2016-12-23T23:06:00 ANTELOPE VALLEY PARKWAY & X ST (STREET) 0 0 Non university affiliate cited/released for No Valid Registration and for Driving Under Suspension. 2016-12-23 23:06:00
16005640 NARCOTICS - POSSESSION 2016-12-23T23:54:00 Closed - Cleared by Arrest-Adult 2016-12-23T23:54:00 ANTELOPE VALLEY PARKWAY & X ST (STREET) 0 0 Non university affiliate cited/released for Headlight Violation, Possession of Paraphernalia, and Possession of Marijuana <1oz. 2016-12-23 23:54:00
16005642 ALCOHOL - DWI 2016-12-24T01:44:00 Closed - Cleared by Arrest-Adult 2016-12-24T01:44:00 Q ST & N 10TH ST (STREET) 0 0 Non-university affiliate was contacted on a traffic stop and cited for DUI < .15 and no headlights after dark. (.084BAC) 2016-12-24 01:44:00
16005645 TRAFFIC - SUSPENDED DRIVER 2016-12-24T14:03:00 Closed - Cleared by Arrest-Adult 2016-12-24T14:03:00 N 14TH ST & P ST (STREET) 0 0 Non-university affiliate contacted on a traffic stop and cited for DUS. Passenger was lodged on two active warrants. 2016-12-24 14:03:00
16005655 NARCOTICS - POSSESSION 2016-12-26T14:34:00 Closed - Cleared by Arrest-Adult 2016-12-26T14:34:00 Pound Hall 513 North 17th St (UNL RESIDENCE HALL) 0 0 Contacted student in their room at Pound Hall where Marijuana, Alcoholic and a Fake ID where found. Student cited for Possession of Marijuana < 1oz, Minor in Possession, Possession of a Fake ID Card and Possession o Drug Paraphernalia. 2016-12-26 14:34:00
16005665 SS - CHECK WELFARE OF PERSON 2016-12-27T21:26:00 Closed - Non-Criminal Case 2016-12-27T21:26:00 The 50-50 1801 R Street (MULTI-DWELLING (7 + UNITS) off campus) 0 0 A report of a check welfare made to UPD in reference to a resident of the 50/50. Attempts to locate the person have been unsuccessful. Investigation is on-going. 2016-12-27 21:26:00
16005666 DISTURBANCE - DOMESTIC 2016-12-27T23:40:00 Closed - Cleared by Arrest-Adult 2016-12-27T22:01:00 2016-12-27T23:40:00 The Village 1055 North 16 St. (UNL APARTMENT) 0 0 A Nebraska student reported a disturbance occurring at their university apartment. A non-affiliate was causing a disturbance outside of the residence and refusing to leave. Investigation continues 2016-12-27 23:40:00
16005675 FIRE - FALSE ALARM 2016-12-28T13:27:00 Closed - Non-Criminal Case 2016-12-28T13:27:00 2016-12-28T13:27:00 Morrill Hall 1335 U St (UNL SUPPORT FACILITY (office, service)) 0 0 Water pipe broke in Morrill Hall causing minor damage. 2016-12-28 13:27:00
16005680 ALCOHOL - DWI 2016-12-29T03:16:00 Closed - Cleared by Arrest-Adult 2016-12-28T23:28:00 N 14TH ST & Q ST (STREET) 0 0 A non-affiliate was contacted on a traffic stop for negligent driving. The driver was cited/released for DUI > .15 (.192 BrAC), Negligent Driving and No Operator's License. 2016-12-29 03:16:00
16005688 ASSAULT - OF POLICE OFFICER 2016-12-29T19:09:00 Closed - Cleared by Arrest-Adult 2016-12-29T19:09:00 2016-12-29T19:09:00 Burr Hall 1701 N 35th St (STREET) 0 0 A non-affiliate was contacted on a check welfare assist for LFR. The non-affiliate was cited/lodged for Assault with Bodily Fluids on a Peace Officer (spit) and cited/released for Trespassing 1st Degree. 2016-12-29 19:09:00
16005696 WEAPONS - FELON IN POSSESSION 2016-12-30T15:14:00 Closed - Cleared by Arrest-Adult 2016-12-30T15:14:00 N ANTELOPE VALLEY PKWY & Q ST (STREET) 0 0 3 non-affiliates were contacted on a traffic stop. The driver was cited/lodged for Possession of a Deadly Weapon by a Prohibited Person and cited/released for Carrying a Concealed Weapon. A passenger was also lodged on an outstanding warrant. 2016-12-30 15:14:00
16005701 TRAFFIC - SUSPENDED DRIVER 2016-12-30T19:54:00 Closed - Cleared by Arrest-Adult 2016-12-30T19:54:00 Nebraska Hall 900 North 16th St (STREET) 0 0 A non-affiliate was cited/released for DUS on a police contact. 2016-12-30 19:54:00
16005702 TRESPASSING 2016-12-30T22:15:00 Closed - Cleared by Arrest-Adult 2016-12-30T22:15:00 Phi Gamma Delta 1425 R St. (OFF CAMPUS/GREEK HOUSING) 0 0 Two University students were cited/released for Trespassing. One student was also cited for Possession of a False ID. 2016-12-30 22:15:00
16005705 NARCOTICS - POSSESSION 2016-12-31T23:04:00 Closed - Cleared by Arrest-Adult 2016-12-31T23:04:00 CHARLESTON ST & N 10TH ST (STREET) 0 0 A UNL student was contacted on a traffic stop ad cited/released for MIP. Additionally, a non-affiliate occupant was cited/released for Possession of Marijuana (12.2 g) < 1 Ounce, Open Alcohol Container, and Speeding (6-10). 2016-12-31 23:04:00

Okay, let's walk through this. By now, you should understand mutating. So the bits that are new are these:

CleanDate = as.POSIXct(Reported, format="%Y-%m-%dT%H:%M")

So CleanDate is the name of the new field we're creating.

The as.POSIXct takes some explaining. And there's a lot more explaining here. But base R has a couple of ways of parsing dates. The most common one you'll see is as.Date which works great, unless you have dates AND times like we do. So you have to use one of two as.POSIX variants, as.POSIXct and as.POSIXlt, and knowing the difference between them isn't useful. So as.POSIXct takes some inputs -- the field you're working with, and format of the date field, using some pattern markers that makes sense when you learn what they are. So our date field is Reported, so that's what we're using to supply the input data. The format of our data, if we look at the output of head above, is YYYY-MM-DDTHH:MM:SS, or year-month-dayThours:minutes:seconds. The oddball here is the T in the middle. If that wasn't there, we'd have an easier time converting this. But it's there, for a reason some developer somewhere decided, and we have to deal with it.

So to parse that pattern, we use a set of markers that tell R what this thing in this spot is. You can get all of the markers that are possible in the strptime documentation, but what we have here represents the most common. So %Y means four digit year (versus %y which is just a two digit year). The rest are pretty self explanator after that, but notice that the dashes, the T and the colons in the time are just typed into it. They too are part of the pattern.

But our question is to count up the total number of incidents per year. So we need to get just a year back, group them together by year and count them. We can do this using format and the same markers as before. It looks like this:


In [5]:
unlcrime %>% mutate(
    year = format(as.POSIXct(Reported, format="%Y-%m-%dT%H:%M"), "%Y"),
)


CaseNoIncident.CodeReportedCase.StatusStart.OccurredEnd.OccurredBuildingLocationStolenDamagedDescriptionyear
11000001 NARCOTICS - POSSESSION 2011-01-01T00:10:00 Cleared by Arrest 2011-01-01T00:10:00 515 N 19th Parking Lot Vine to 515 North 19th St. (GOV'T PARKING LOT (UNL)) NA NA UNL student cited and released for Poss of Marijuana < 1 oz and Poss of Drug Paraphernalia after a CSO reported suspicious drug-related activity from a vehicle. 1.2 g of marijuana seized. 2011
11000011 NARCOTICS - POSSESSION 2011-01-01T23:16:00 Cleared by Arrest 2011-01-01T23:16:00 N 10TH ST & CHARLESTON ST (STREET) NA NA Traffic Stop. Vehicle stopped for a headlight out. Upon contact Officer smelled the strong odor of burnt marijuana. Driver cited/released for Possession of Drug Paraphernalia after a search yielded a pipe with burnt marijuana residue. 2011
11000013 ALCOHOL - MINOR IN POSSESSION 2011-01-02T00:38:00 Cleared by Arrest 2011-01-02T00:38:00 N 17TH ST & N ANTELOPE VALLEY PKWY (STREET) NA NA Three persons cited for MIPC after being stopped for no headlights at N. Antelope valley parkway and N. 17rh Street. 2011
11000019 TRAFFIC - SUSPENDED DRIVER 2011-01-02T23:24:00 Cleared by Arrest 2011-01-02T23:24:00 N 17TH ST & VINE ST (STREET) NA NA Non-student cited and lodged in jail for Driving Under Suspension after being stopped for speeding. 2011
11000020 NARCOTICS - POSSESSION 2011-01-03T04:46:00 Cleared by Arrest 2011-01-03T04:46:00 N 10TH ST & CHARLESTON ST (STREET) NA NA Non-student stopped for speeding. Officer smelled burnt marijuana from within the vehicle and upon searching located burnt marijuana cigarettes, a pipe, and .6 g of marijuana. Cited and released for Speeding, Poss of Marijuana < 1 oz, and Poss of Drug Paraphernalia.2011
11000022 MEDICAL EMERGENCY 2011-01-03T10:34:00 Inactive 2011-01-03T10:34:00 Campus Recreation Center 841 North 14th St (SPORT FACILITIES) NA NA UNL affiliated female disoriented in need of medical attention at the Rec Center. Transported to the hospital by LFR. 2011
11000024 FRAUD - CREDIT CARDS/ATM/BANK CARD 2011-01-03T14:25:00 Inactive 2010-12-21T00:00:00 Cyberspace/Internet (INTERNET/CYBERSPACE) NA NA Person received items they did not order on their credit card, unknown who may have used their card. 2011
11000027 FIRE (WORKING) - ALARM 2011-01-03T20:51:00 Cleared by Exception 2011-01-03T20:51:00 Memorial Stadium 740 Stadium Dr. (SPORT FACILITIES) NA NA Fire alarm at Memorial Stadium caused by broken water pipe, no damage at this time, LFR on scene. 2011
11000030 LARCENY - MOTOR VEH. ACCESSORIES 2011-01-04T10:57:00 Inactive 2010-12-03T17:00:00 2011-01-04T10:57:00 Hardin Hall 1600 North 33rd St (GOV'T PARKING LOT (UNL)) 400 500 Theft of a catalytic converter from a state owned vehicle. 2011
11000032 DISTURBANCE - OTHER 2011-01-04T16:40:00 Inactive Burnett Hall 1220 T St (UNL ACADEMIC BUILDING) NA NA Person received a letter from another person which disturbed them. 2011
11000033 PROPERTY DAMAGE - UNINTENTIONAL,NON TRAFFIC 2011-01-04T17:18:00 Cleared by Exception 2011-01-04T08:00:00 2011-01-04T17:00:00 Stadium Loop Parking Lot (GOV'T PARKING LOT (UNL)) NA NA Mirror had paint traces on it from another vehicle touching the mirror while backing out of stall, no damage. This occured in the area 10 Loop 14th and Vine Street. 2011
11000036 TRESPASSING 2011-01-04T21:43:00 Cleared by Exception 2011-01-04T21:43:00 Oldfather Hall 660 North 12th St (UNL ACADEMIC BUILDING) NA NA Non-student banned from University buildings after viewing pornography on computers. 2011
11000044 DISTURBANCE - OTHER 2011-01-06T08:43:00 Inactive 2011-01-03T12:53:00 (INTERNET/CYBERSPACE) NA NA UNL staff reported receiving unwanted emails. 2011
11000048 FIRE (WORKING) - ALARM 2011-01-06T17:18:00 Cleared by Exception 2011-01-06T17:18:00 2244 Vine Street 2244 Vine Street (UNL APARTMENT) NA 15 LFR responded to a fire on the balcony of building, 3rd floor. Later found resident had been smoking on the balcony and put a cigarette butt in flower pot causing it's contents to start on fire. Damage to flower pot, no damage to structure. 2011
11000049 OUTSIDE - O.P.S. OTHER 2011-01-06T17:44:00 Inactive 2011-01-06T16:00:00 2011-01-06T17:44:00 3201 W Street (SIDEWALK) 300 NA Non UNL affiliated person had $300.00 stolen. This occurred off campus and was tunred over to LPD. 2011
11000058 TRESPASSING 2011-01-07T10:00:00 Cleared by Exception 2011-01-07T10:00:00 Love Library South 1248 R (LIBRARY) NA NA Transient taken to detox after he passed out in Love Library (PBT refused). He was issued a Ban and Bar Letter. 2011
11000061 PROPERTY DAMAGE - UNINTENTIONAL,NON TRAFFIC 2011-01-07T14:17:00 Cleared by Exception 2011-01-07T14:17:00 Lied Center 301 North 12th St (UNL SUPPORT FACILITY (office, service)) NA 350 Wind blew a dumpster into a vehicle owned by UNL ($350 damage). 2011
11000063 TRAFFIC - OTHER 2011-01-08T02:50:00 Cleared by Arrest 2011-01-08T02:50:00 N 17TH ST & R ST (STREET) NA NA UNL student stopped for expired license plates and found to have consumed alcohol. Ultimately tested under legal limit. Taken to Detox and cited for Reckless Driving. .072 BAC 2011
11000065 LARCENY - STOLEN BIKE 2011-01-08T10:40:00 Inactive Westbrook Music Building 1104 R St (UNL ACADEMIC BUILDING) 50 NA Black and green Gary Fischer bicycle stolen sometime in September 2010. 2011
11000068 MEDICAL EMERGENCY 2011-01-08T14:46:00 Cleared by Exception 2011-01-08T14:46:00 Bob Devaney Sports Center 1600 Court St (SPORT FACILITIES) NA NA Non UNL affiliated male suffered a seizure while attending a basketball game. 2011
11000070 OUTSIDE - O.P.S. OTHER 2011-01-08T19:35:00 Cleared by Exception 2011-01-03T08:00:00 2011-01-08T19:30:00 1525 South 19th #5 (DUPLEX ___���� off campus) NA NA UNL student reported being concerned about the behavior of an ex-boyfriend. 2011
11000071 ALCOHOL - DWI 2011-01-09T02:21:00 Cleared by Arrest 2011-01-09T02:21:00 X ST & N 16TH ST (STREET) NA NA 4 female UNL students contacted after a traffic infraction. 1 cited for DUI, 3 cited for MIPC, open container and false information. BAC. for driver .181. 2011
11000072 ALCOHOL - MINOR IN POSSESSION 2011-01-09T03:05:00 Cleared by Arrest 2011-01-09T03:05:00 Robert E. Knoll Residence Center 440 N 17th Street (UNL RESIDENCE HALL) NA NA CSO reported possible alcohol activity. Four UNL students cited and released for MIP after alcohol was observed in the room. .089, .049, .055, & .176 BAC's 2011
11000073 NARCOTICS - POSSESSION 2011-01-09T10:20:00 Inactive 2011-01-07T18:00:00 Robert E. Knoll Residence Center 440 N 17th Street (UNL RESIDENCE HALL) NA NA Marijuana pipe found under a couch cushion in the room while new residents were moving in. 2011
11000076 PROPERTY DAMAGE - UNINTENTIONAL,NON TRAFFIC 2011-01-10T08:37:00 Cleared by Exception 2011-01-10T08:37:00 College of Dentistry 3975 Potter St (GOV'T PARKING LOT (UNL)) NA 800 UNL owned snow blower struck a parked vehicle. 2011
11000078 PROPERTY DAMAGE - UNINTENTIONAL,NON TRAFFIC 2011-01-10T11:10:00 Cleared by Exception 2011-01-10T11:10:00 Pound Hall 513 North 17th St (SIDEWALK) NA 100 UNL snow plow struck a meter while clearing the sidewalk of snow causing it to slide into a parked vehicle. Damage to City of Lincoln meter (Bent over) and parked vehicle (Driver's side mirror). 2011
11000082 LARCENY - FROM MOTOR VEHICLE 2011-01-10T14:47:00 Inactive 2010-12-15T12:00:00 2011-01-07T16:00:00 Husker Hall 705 North 23rd St (GOV'T PARKING LOT (UNL)) 780 50 Belated report of a laptop computer and Sony digital recorder stolen from vehicle. Pry marks found on passenger side door frame. Occurred between December 15th, 2010 to January 7th, 2011. 2011
11000083 LOST OR STOLEN ITEM 2011-01-10T16:43:00 Inactive 2011-01-09T13:30:00 2011-01-09T15:00:00 Campus Recreation Center 841 North 14th St (SPORT FACILITIES) 43 NA Black bi-fold wallet and contents taken from an unsecured locker in the men's locker room. Debit/Credit cards were canceled by victim. 2011
11000085 NARCOTICS - POSSESSION 2011-01-10T23:24:00 Cleared by Arrest 2011-01-10T23:24:00 2011-01-10T23:24:00 Abel Hall 880 North 17th St (UNL RESIDENCE HALL) NA NA Person cited for Possession of Marijuana less than an ounce, this occured in Abel Hall. 2011
11000086 SS - OTHER 2011-01-11T00:53:00 Inactive 2011-01-11T00:53:00 Selleck Quad 4000 600 North 15th St. (UNL RESIDENCE HALL) NA NA Safe found unlocked in Selleck Quad, contents inventoried and safe locked. 2011
16005585 ACCIDENTS - P.D. REPORTABLE 2016-12-19T14:04:00 Closed - Inactive 2016-12-19T14:04:00 Business Administration Building 512 North 12th St (GOV'T PARKING LOT (UNL)) 0 100 Two vehicle non injury accident involving a UNL vehicle, dock of Love Library. 2016
16005588 ACCIDENTS - P.D. NOT REPORTABLE 2016-12-19T21:53:00 Closed - Non-Criminal Case 2016-12-19T21:44:00 440 N 17th Street (GOV'T PARKING LOT (UNL)) 0 550 A Nebraska student reported they had hit a sign with their vehicle. 2016
16005598 ALCOHOL - DWI 2016-12-20T16:03:00 Closed - Cleared by Arrest-Adult 2016-12-20T16:03:00 N 17TH ST & Q ST (GAS/SERVICE STATION) 0 0 Student contacted at 17th and Q Street, found to be under the influence of drugs. Cited for DUI-D and Refusing a Chemical Test. 2016
16005599 TRAFFIC - SUSPENDED DRIVER 2016-12-20T20:09:00 Closed - Cleared by Arrest-Adult 2016-12-20T20:09:00 N ANTELOPE VALLEY PKWY & Salt Creek RDWY (STREET) 0 0 Vehicle stopped at 21st and SCR, driver found to be suspend. Driver cited for DUS and Improper turn and released. 2016
16005607 NARCOTICS - POSSESSION 2016-12-21T16:34:00 Closed - Cleared by Arrest-Adult 2016-12-21T16:34:00 N 17TH ST & Q ST (STREET) 0 0 Vehicle stopped for no plates or paper. Marijuana was found in the vehicle. Driver cited for Possession of Marijuana and Possession of Drug Paraphernalia. 2016
16005609 TRAFFIC - SUSPENDED DRIVER 2016-12-21T19:14:00 Closed - Cleared by Arrest-Adult 2016-12-21T19:14:00 N ANTELOPE VALLEY PKWY & Salt Creek RDWY (STREET) 0 0 A vehicle was stopped for a traffic violation. A non-affiliate was cited/lodged for DUS and cited for Failure to signal turn. 2016
16005611 ALCOHOL - MINOR IN POSSESSION 2016-12-21T20:37:00 Closed - Cleared by Arrest-Adult 2016-12-21T20:37:00 University Suites 1780 "R" Street (GOV'T PARKING LOT (UNL)) 0 0 A student and a non-affiliate were contacted in a vehicle due to suspicious activity. Both were cited/released for MIP and the student was also cited/released for Possession of Marijuana < 1 oz. 2016
16005613 ARSON 2016-12-21T22:02:00 Closed - Cleared by Arrest-Adult 2016-12-21T09:43:00 N ANTELOPE VALLEY PKWY & Salt Creek RDWY (OTHER LOT/FIELD) 0 0 A report of a working fire set intentionally by an unknown person. Investigation continues. 2016
16005616 NARCOTICS - POSSESSION 2016-12-22T01:38:00 Closed - Cleared by Arrest-Adult 2016-12-22T01:38:00 ANTELOPE VALLEY PARKWAY & Q ST (STREET) 0 0 Officer identified driver to be suspended using MDT. Driver was cited/lodged for Possession of a Controlled Substance. Cited/released for Possession of Substance, DUS, Open Container, Possession of Marijuana < 1oz. and Possession of Drug Paraphernalia. Passenger cited/lodged for Possession of a Controlled Substance. Cited/released for Possession of Marijuana < 1 oz and Possession of Drug Paraphernalia. Both are non-affiliates.2016
16005617 VANDALISM - OTHER 2016-12-22T05:35:00 Closed - Cleared by Arrest-Adult Nebraska Champions Club 707 Stadium Dr (GOV'T PARKING LOT (UNL)) 0 25 The inner pan of a trash receptacle was found to be damaged. 2016
16005633 OUTSIDE - O.P.S. OTHER 2016-12-22T07:19:00 Closed - Inactive (INTERNET/CYBERSPACE) 0 0 University of Nebraska reported a server breach, case turned over to FBI. 2016
16005627 TRAFFIC - SUSPENDED DRIVER 2016-12-22T22:52:00 Closed - Cleared by Arrest-Adult 2016-12-22T22:52:00 ANTELOPE VALLEY PARKWAY & SALT CREEK RD (STREET) 0 0 Non-affiliate was lodged in jail for Driving Under Suspension and cited for narcotic offenses. 2016
16005629 ASSAULT - DOMESTIC 2016-12-23T00:28:00 Closed - Cleared by Arrest-Adult 2016-12-23T00:28:00 Beadle Center 1901 Vine St (UNL ACADEMIC BUILDING) 0 0 University affiliate was lodged in jail for 3rd Degree Domestic Assault. 2016
16005630 ALCOHOL - DRUNK 2016-12-23T01:46:00 Closed - Non-Criminal Case 2016-12-23T01:46:00 N 8TH ST & Q ST (SIDEWALK) 0 0 Non affiliate contacted and taken to The Bridge (.214 BAC). 2016
16005636 TRAFFIC - SUSPENDED DRIVER 2016-12-23T18:34:00 Closed - Cleared by Arrest-Adult 2016-12-23T18:34:00 N ANTELOPE VALLEY PKWY & MILITARY RD (STREET) 0 0 A non-affiliate was contacted due to suspended driving privileges. The driver was cited/released for DUS. 2016
16005638 ALCOHOL - DWI 2016-12-23T22:25:00 Closed - Cleared by Arrest-Adult 2016-12-23T22:25:00 P ST & N 10TH ST (STREET) 0 0 Non university affiliate was cited for DUI <.15 (.105 BAC) and lodged for Criminal Impersonation. 2016
16005639 TRAFFIC - SUSPENDED DRIVER 2016-12-23T23:06:00 Closed - Cleared by Arrest-Adult 2016-12-23T23:06:00 ANTELOPE VALLEY PARKWAY & X ST (STREET) 0 0 Non university affiliate cited/released for No Valid Registration and for Driving Under Suspension. 2016
16005640 NARCOTICS - POSSESSION 2016-12-23T23:54:00 Closed - Cleared by Arrest-Adult 2016-12-23T23:54:00 ANTELOPE VALLEY PARKWAY & X ST (STREET) 0 0 Non university affiliate cited/released for Headlight Violation, Possession of Paraphernalia, and Possession of Marijuana <1oz. 2016
16005642 ALCOHOL - DWI 2016-12-24T01:44:00 Closed - Cleared by Arrest-Adult 2016-12-24T01:44:00 Q ST & N 10TH ST (STREET) 0 0 Non-university affiliate was contacted on a traffic stop and cited for DUI < .15 and no headlights after dark. (.084BAC) 2016
16005645 TRAFFIC - SUSPENDED DRIVER 2016-12-24T14:03:00 Closed - Cleared by Arrest-Adult 2016-12-24T14:03:00 N 14TH ST & P ST (STREET) 0 0 Non-university affiliate contacted on a traffic stop and cited for DUS. Passenger was lodged on two active warrants. 2016
16005655 NARCOTICS - POSSESSION 2016-12-26T14:34:00 Closed - Cleared by Arrest-Adult 2016-12-26T14:34:00 Pound Hall 513 North 17th St (UNL RESIDENCE HALL) 0 0 Contacted student in their room at Pound Hall where Marijuana, Alcoholic and a Fake ID where found. Student cited for Possession of Marijuana < 1oz, Minor in Possession, Possession of a Fake ID Card and Possession o Drug Paraphernalia. 2016
16005665 SS - CHECK WELFARE OF PERSON 2016-12-27T21:26:00 Closed - Non-Criminal Case 2016-12-27T21:26:00 The 50-50 1801 R Street (MULTI-DWELLING (7 + UNITS) off campus) 0 0 A report of a check welfare made to UPD in reference to a resident of the 50/50. Attempts to locate the person have been unsuccessful. Investigation is on-going. 2016
16005666 DISTURBANCE - DOMESTIC 2016-12-27T23:40:00 Closed - Cleared by Arrest-Adult 2016-12-27T22:01:00 2016-12-27T23:40:00 The Village 1055 North 16 St. (UNL APARTMENT) 0 0 A Nebraska student reported a disturbance occurring at their university apartment. A non-affiliate was causing a disturbance outside of the residence and refusing to leave. Investigation continues 2016
16005675 FIRE - FALSE ALARM 2016-12-28T13:27:00 Closed - Non-Criminal Case 2016-12-28T13:27:00 2016-12-28T13:27:00 Morrill Hall 1335 U St (UNL SUPPORT FACILITY (office, service)) 0 0 Water pipe broke in Morrill Hall causing minor damage. 2016
16005680 ALCOHOL - DWI 2016-12-29T03:16:00 Closed - Cleared by Arrest-Adult 2016-12-28T23:28:00 N 14TH ST & Q ST (STREET) 0 0 A non-affiliate was contacted on a traffic stop for negligent driving. The driver was cited/released for DUI > .15 (.192 BrAC), Negligent Driving and No Operator's License. 2016
16005688 ASSAULT - OF POLICE OFFICER 2016-12-29T19:09:00 Closed - Cleared by Arrest-Adult 2016-12-29T19:09:00 2016-12-29T19:09:00 Burr Hall 1701 N 35th St (STREET) 0 0 A non-affiliate was contacted on a check welfare assist for LFR. The non-affiliate was cited/lodged for Assault with Bodily Fluids on a Peace Officer (spit) and cited/released for Trespassing 1st Degree. 2016
16005696 WEAPONS - FELON IN POSSESSION 2016-12-30T15:14:00 Closed - Cleared by Arrest-Adult 2016-12-30T15:14:00 N ANTELOPE VALLEY PKWY & Q ST (STREET) 0 0 3 non-affiliates were contacted on a traffic stop. The driver was cited/lodged for Possession of a Deadly Weapon by a Prohibited Person and cited/released for Carrying a Concealed Weapon. A passenger was also lodged on an outstanding warrant. 2016
16005701 TRAFFIC - SUSPENDED DRIVER 2016-12-30T19:54:00 Closed - Cleared by Arrest-Adult 2016-12-30T19:54:00 Nebraska Hall 900 North 16th St (STREET) 0 0 A non-affiliate was cited/released for DUS on a police contact. 2016
16005702 TRESPASSING 2016-12-30T22:15:00 Closed - Cleared by Arrest-Adult 2016-12-30T22:15:00 Phi Gamma Delta 1425 R St. (OFF CAMPUS/GREEK HOUSING) 0 0 Two University students were cited/released for Trespassing. One student was also cited for Possession of a False ID. 2016
16005705 NARCOTICS - POSSESSION 2016-12-31T23:04:00 Closed - Cleared by Arrest-Adult 2016-12-31T23:04:00 CHARLESTON ST & N 10TH ST (STREET) 0 0 A UNL student was contacted on a traffic stop ad cited/released for MIP. Additionally, a non-affiliate occupant was cited/released for Possession of Marijuana (12.2 g) < 1 Ounce, Open Alcohol Container, and Speeding (6-10). 2016

So, now we can add the group by and summarize steps to that, and we get our answer.


In [6]:
unlcrime %>% mutate(
    year = format(as.POSIXct(Reported, format="%Y-%m-%dT%H:%M"), "%Y"),
) %>% group_by(year) %>% summarize(
    count = n()
)


yearcount
20111948
20121881
20132052
20141920
20151956
20161820

When it's not weird, Lubridate

There is a library called lubridate that does some of this parsing for you. Let's get some of the bits we're familiar with out of the way. First we'll open the library, then import a dataset of parking tickets.


In [7]:
library(lubridate)


Attaching package: ‘lubridate’

The following object is masked from ‘package:base’:

    date


In [8]:
tickets <- read.csv("../../Data/tickets.csv", stringsAsFactors = FALSE)

In [9]:
head(tickets)


CitationDateLocationViolation
15078429 2012-04-02 07:15:00 North Stadium Expired Meter
24048318 2012-04-02 07:22:00 Housing No Valid Permit Displayed
24048320 2012-04-02 07:26:00 14th & W Street No Valid Permit Displayed
15078430 2012-04-02 07:36:00 Champions Club Parking in Unauthorized Area
18074937 2012-04-02 07:39:00 Sandoz Expired Meter
18074938 2012-04-02 07:40:00 Sandoz Expired Meter

This is easy enough that it barely requires explanation. If your date format matches one of lubridate's patterns, you can just apply that pattern. It's just this simple.


In [10]:
tickets %>% mutate(
    CleanDate = ymd_hms(Date)
)


CitationDateLocationViolationCleanDate
15078429 2012-04-02 07:15:00 North Stadium Expired Meter 2012-04-02 07:15:00
24048318 2012-04-02 07:22:00 Housing No Valid Permit Displayed 2012-04-02 07:22:00
24048320 2012-04-02 07:26:00 14th & W Street No Valid Permit Displayed 2012-04-02 07:26:00
15078430 2012-04-02 07:36:00 Champions Club Parking in Unauthorized Area2012-04-02 07:36:00
18074937 2012-04-02 07:39:00 Sandoz Expired Meter 2012-04-02 07:39:00
18074938 2012-04-02 07:40:00 Sandoz Expired Meter 2012-04-02 07:40:00
18074939 2012-04-02 07:41:00 Sandoz Expired Meter 2012-04-02 07:41:00
18074940 2012-04-02 07:42:00 Sandoz Parking in Unauthorized Area2012-04-02 07:42:00
18074941 2012-04-02 07:43:00 Sandoz Displaying Altered Permit 2012-04-02 07:43:00
18074942 2012-04-02 07:45:00 Sandoz Displaying Altered Permit 2012-04-02 07:45:00
18074943 2012-04-02 07:47:00 Sandoz No Valid Permit Displayed 2012-04-02 07:47:00
18074944 2012-04-02 07:49:00 Sandoz Displaying Altered Permit 2012-04-02 07:49:00
18074945 2012-04-02 07:49:00 Sandoz Parking in Unauthorized Area2012-04-02 07:49:00
18074946 2012-04-02 07:51:00 Sandoz Parking in Unauthorized Area2012-04-02 07:51:00
18074947 2012-04-02 07:52:00 Sandoz Parking in Unauthorized Area2012-04-02 07:52:00
18074948 2012-04-02 07:56:00 Abel Expired Meter 2012-04-02 07:56:00
18074949 2012-04-02 07:57:00 Abel Expired Meter 2012-04-02 07:57:00
18074950 2012-04-02 07:59:00 Abel Expired Meter 2012-04-02 07:59:00
22067748 2012-04-02 08:14:00 Dental Patient Parking in Unauthorized Area2012-04-02 08:14:00
22067749 2012-04-02 08:16:00 Dental Patient Parking in Unauthorized Area2012-04-02 08:16:00
15078431 2012-04-02 08:19:00 South Stadium Expired Meter 2012-04-02 08:19:00
22067750 2012-04-02 08:25:00 Dental Patient Parking in Unauthorized Area2012-04-02 08:25:00
22067751 2012-04-02 08:26:00 Dental Patient Parking in Unauthorized Area2012-04-02 08:26:00
22067752 2012-04-02 08:28:00 Dental Patient Parking in Unauthorized Area2012-04-02 08:28:00
22067753 2012-04-02 08:29:00 Dental Patient Parking in Unauthorized Area2012-04-02 08:29:00
15078432 2012-04-02 08:29:00 North Stadium Displaying Altered Permit 2012-04-02 08:29:00
22067754 2012-04-02 08:30:00 Dental Patient Parking in Unauthorized Area2012-04-02 08:30:00
16085595 2012-04-02 08:33:00 Union Expired Meter 2012-04-02 08:33:00
16085596 2012-04-02 08:34:00 Union Expired Meter 2012-04-02 08:34:00
16085597 2012-04-02 08:35:00 Union Expired Meter 2012-04-02 08:35:00
EF0513984 2016-07-01 07:50:01 Inside Loop No Valid Permit Displayed 2016-07-01 07:50:01
EF0513989 2016-07-01 08:23:28 Loop No Valid Permit Displayed 2016-07-01 08:23:28
EF0612686 2016-07-01 08:43:45 Boathouse No Valid Permit Displayed 2016-07-01 08:43:45
EF0812817 2016-07-01 08:45:28 Champions Club No Valid Permit Displayed 2016-07-01 08:45:28
EF0612687 2016-07-01 08:50:27 Boathouse No Valid Permit Displayed 2016-07-01 08:50:27
EF0313642 2016-07-01 08:50:47 East Union Expired Meter 2016-07-01 08:50:47
EF0513997 2016-07-01 09:07:55 Harper Dining Center No Valid Permit Displayed 2016-07-01 09:07:55
EF0514003 2016-07-01 09:30:44 Andersen No Valid Permit Displayed 2016-07-01 09:30:44
EF0313663 2016-07-01 09:33:41 Plant Science No Valid Permit Displayed 2016-07-01 09:33:41
EF0313670 2016-07-01 09:47:07 Insectary No Valid Permit Displayed 2016-07-01 09:47:07
EF0313687 2016-07-01 10:18:53 C.Y. Thompson No Valid Permit Displayed 2016-07-01 10:18:53
EF0313707 2016-07-01 11:16:00 Barkley Patient No Valid Permit Displayed 2016-07-01 11:16:00
EF0313709 2016-07-01 11:22:39 Barkley Patient No Valid Permit Displayed 2016-07-01 11:22:39
EF0812837 2016-07-01 11:46:12 401 N 9th Street No Valid Permit Displayed 2016-07-01 11:46:12
EF0612718 2016-07-05 13:37:19 Stadium Drive Parking GarageFailure to Pay[on exit] 2016-07-05 13:37:19
EF0812859 2016-07-05 16:07:15 Inside Loop No Valid Permit Displayed 2016-07-05 16:07:15
EF0416338 2016-07-06 12:08:41 Loop No Valid Permit Displayed 2016-07-06 12:08:41
EF0111669 2016-07-07 14:24:44 Loop No Valid Permit Displayed 2016-07-07 14:24:44
EF0812867 2016-07-07 17:24:53 14th & W Street Expired Meter 2016-07-07 17:24:53
EF0313796 2016-07-08 09:55:07 Inside Loop Parking in Unauthorized Area2016-07-08 09:55:07
EF0416345 2016-07-08 08:33:01 East Union No Valid Permit Displayed 2016-07-08 08:33:01
EF0313811 2016-07-08 10:36:37 Andersen No Valid Permit Displayed 2016-07-08 10:36:37
EF0313812 2016-07-08 10:37:47 Andersen No Valid Permit Displayed 2016-07-08 10:37:47
EF0313823 2016-07-08 13:08:36 13th & R Lot Expired Meter 2016-07-08 13:08:36
EF0313824 2016-07-08 13:09:37 13th & R Lot Expired Meter 2016-07-08 13:09:37
EF0713528 2016-07-08 13:10:00 14th & W Street Expired Meter 2016-07-08 13:10:00
EF0212048 2016-07-12 10:23:33 Loop No Valid Permit Displayed 2016-07-12 10:23:33
EF0212049 2016-07-12 10:27:06 Loop No Valid Permit Displayed 2016-07-12 10:27:06
EF0212066 2016-07-12 11:07:09 Inside Loop No Valid Permit Displayed 2016-07-12 11:07:09
EF0416371 2016-07-13 10:33:40 South Stadium Expired Meter 2016-07-13 10:33:40

Now it might not look like anything is different, but it is. CleanDate can now be used as dates, you can do date math with it, you can parse it to get the year or month or whatever you need. It becomes useful data.

Assignment

Load in this dataset of all assaults on Lincoln Police officers and answer this question: What month do most assaults on police officers happen?

Rubric

  1. Did you load the data correctly?
  2. Did you apply the right date parsing formula?
  3. Did you parse the month out of the formatted data?
  4. Did you do the group and count correctly?
  5. Did you describe your steps in Markdown comments?

In [ ]: