How many rows of data (observations) are in this dataset?
Sys.setlocale("LC_ALL","C")
[1] "C"
Warning message:
In scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
EOF within quoted string
191641
[1] 191641
How many variables are in this dataset?
11
[1] 11
Using the “max” function, what is the maximum value of the variable “ID”?
max(mvtWeek1$ID)
[1] 9181151
What is the minimum value of the variable “Beat”?
min(mvtWeek1$Beat)
[1] 111
How many observations have value TRUE in the Arrest variable (this is the number of crimes for which an arrest was made)?
sum(mvtWeek1$Arrest)
[1] 15536
How many observations have a LocationDescription value of ALLEY?
table(mvtWeek1$LocationDescription)
ABANDONED BUILDING
4
AIRPORT BUILDING NON-TERMINAL - NON-SECURE AREA
4
AIRPORT BUILDING NON-TERMINAL - SECURE AREA
1
AIRPORT EXTERIOR - NON-SECURE AREA
24
AIRPORT EXTERIOR - SECURE AREA
1
AIRPORT PARKING LOT
11
AIRPORT TERMINAL UPPER LEVEL - NON-SECURE AREA
5
AIRPORT VENDING ESTABLISHMENT
10
AIRPORT/AIRCRAFT
363
ALLEY
2308
ANIMAL HOSPITAL
1
APARTMENT
184
APPLIANCE STORE
1
ATHLETIC CLUB
9
BANK
7
BAR OR TAVERN
17
BARBERSHOP
4
BOWLING ALLEY
3
BRIDGE
2
CAR WASH
44
CHA APARTMENT
5
CHA PARKING LOT/GROUNDS
405
CHURCH/SYNAGOGUE/PLACE OF WORSHIP
56
CLEANING STORE
3
COLLEGE/UNIVERSITY GROUNDS
47
COLLEGE/UNIVERSITY RESIDENCE HALL
2
COMMERCIAL / BUSINESS OFFICE
126
CONSTRUCTION SITE
35
CONVENIENCE STORE
7
CTA GARAGE / OTHER PROPERTY
148
CTA TRAIN
1
CURRENCY EXCHANGE
2
DAY CARE CENTER
5
DEPARTMENT STORE
22
DRIVEWAY - RESIDENTIAL
1675
DRUG STORE
8
FACTORY/MANUFACTURING BUILDING
16
FIRE STATION
5
FOREST PRESERVE
6
GAS STATION
2111
GOVERNMENT BUILDING/PROPERTY
48
GROCERY FOOD STORE
80
HIGHWAY/EXPRESSWAY
22
HOSPITAL BUILDING/GROUNDS
101
HOTEL/MOTEL
124
JAIL / LOCK-UP FACILITY
1
LAKEFRONT/WATERFRONT/RIVERBANK
4
LIBRARY
4
MEDICAL/DENTAL OFFICE
3
MOVIE HOUSE/THEATER
18
NEWSSTAND
1
NURSING HOME/RETIREMENT HOME
21
OTHER
4573
OTHER COMMERCIAL TRANSPORTATION
8
OTHER RAILROAD PROP / TRAIN DEPOT
28
PARK PROPERTY
255
PARKING LOT/GARAGE(NON.RESID.)
14852
POLICE FACILITY/VEH PARKING LOT
266
RESIDENCE
1302
RESIDENCE PORCH/HALLWAY
18
RESIDENCE-GARAGE
1176
RESIDENTIAL YARD (FRONT/BACK)
1536
RESTAURANT
49
SAVINGS AND LOAN
4
SCHOOL, PRIVATE, BUILDING
14
SCHOOL, PRIVATE, GROUNDS
23
SCHOOL, PUBLIC, BUILDING
114
SCHOOL, PUBLIC, GROUNDS
206
SIDEWALK
462
SMALL RETAIL STORE
33
SPORTS ARENA/STADIUM
166
STREET
156564
TAVERN/LIQUOR STORE
14
TAXICAB
21
VACANT LOT/LAND
985
VEHICLE NON-COMMERCIAL
817
VEHICLE-COMMERCIAL
23
WAREHOUSE
17
In many datasets, like this one, you have a date field. Unfortunately, R does not automatically recognize entries that look like dates. We need to use a function in R to extract the date and time. Take a look at the first entry of Date (remember to use square brackets when looking at a certain entry of a variable).
In what format are the entries in the variable Date?
"Month/Day/Year Hour:Minute"
[1] "Month/Day/Year Hour:Minute"
Now, let’s convert these characters into a Date object in R. In your R console, type
DateConvert = as.Date(strptime(mvt$Date, "%m/%d/%y %H:%M"))
This converts the variable “Date” into a Date object in R. Take a look at the variable DateConvert using the summary function.
What is the month and year of the median date in our dataset? Enter your answer as “Month Year”, without the quotes. (Ex: if the answer was 2008-03-28, you would give the answer “March 2008”, without the quotes.)
DateConvert = as.Date(strptime(mvtWeek1$Date, "%m/%d/%y %H:%M"))
summary(DateConvert)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
NA NA NA NA NA NA "191641"
"May 2006"
[1] "May 2006"
Now, let’s extract the month and the day of the week, and add these variables to our data frame mvt. We can do this with two simple functions. Type the following commands in R:
mvt$Month = months(DateConvert)
mvt$Weekday = weekdays(DateConvert)
This creates two new variables in our data frame, Month and Weekday, and sets them equal to the month and weekday values that we can extract from the Date object. Lastly, replace the old Date variable with DateConvert by typing:
mvt$Date = DateConvert
Using the table command, answer the following questions.
In which month did the fewest motor vehicle thefts occur?
mvtWeek1$Month = months(DateConvert)
mvtWeek1$Weekday = weekdays(DateConvert)
mvtWeek1$Date = DateConvert
table(mvtWeek1$Month)
< table of extent 0 >
"February"
[1] "February"
On which weekday did the most motor vehicle thefts occur?
table(mvtWeek1$Weekday)
< table of extent 0 >
"Friday"
[1] "Friday"
Each observation in the dataset represents a motor vehicle theft, and the Arrest variable indicates whether an arrest was later made for this theft. Which month has the largest number of motor vehicle thefts for which an arrest was made?
table(mvtWeek1$Month,mvtWeek1$Arrest)
< table of extent 0 x 2 >
table(mvtWeek1$Arrest,mvtWeek1$Month)
< table of extent 2 x 0 >
"January"
[1] "January"
Now, let’s make some plots to help us better understand how crime has changed over time in Chicago. Throughout this problem, and in general, you can save your plot to a file. For more information, this website very clearly explains the process.
First, let’s make a histogram of the variable Date. We’ll add an extra argument, to specify the number of bars we want in our histogram. In your R console, type
hist(mvt$Date, breaks=100)
hist(mvtWeek1$Date, breaks=100)
no non-missing arguments to min; returning Infno non-missing arguments to max; returning -InfError in hist.default(unclass(x), unclass(breaks), plot = FALSE, warn.unused = FALSE, :
character(0)
Looking at the histogram, answer the following questions.
In general, does it look like crime increases or decreases from 2002 - 2012?
"Decrease"
[1] "Decrease"
In general, does it look like crime increases or decreases from 2005 - 2008?
"Decrease"
[1] "Decrease"
Now, let’s see how arrests have changed over time. Create a boxplot of the variable “Date”, sorted by the variable “Arrest” (if you are not familiar with boxplots and would like to learn more, check out this tutorial). In a boxplot, the bold horizontal line is the median value of the data, the box shows the range of values between the first quartile and third quartile, and the whiskers (the dotted lines extending outside the box) show the minimum and maximum values, excluding any outliers (which are plotted as circles). Outliers are defined by first computing the difference between the first and third quartile values, or the height of the box. This number is called the Inter-Quartile Range (IQR). Any point that is greater than the third quartile plus the IQR or less than the first quartile minus the IQR is considered an outlier.
Does it look like there were more crimes for which arrests were made in the first half of the time period or the second half of the time period? (Note that the time period is from 2001 to 2012, so the middle of the time period is the beginning of 2007.)
boxplot(mvtWeek1$Date ~ mvtWeek1$Arrest)
"First half"
[1] "First half"
Let’s investigate this further. Use the table function for the next few questions.
For what proportion of motor vehicle thefts in 2001 was an arrest made?
Note: in this question and many others in the course, we are asking for an answer as a proportion. Therefore, your answer should take a value between 0 and 1.
table(mvtWeek1$Arrest, mvtWeek1$Year)
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
FALSE 18517 16638 14859 15169 14956 14796 13068 13425 11327 14796 15012 13542
TRUE 2152 2115 1798 1693 1528 1302 1212 1020 840 701 625 550
2152/(2152+18517)
[1] 0.1041173
For what proportion of motor vehicle thefts in 2007 was an arrest made?
table(mvtWeek1$Year,mvtWeek1$Arrest)
FALSE TRUE
2001 18517 2152
2002 16638 2115
2003 14859 1798
2004 15169 1693
2005 14956 1528
2006 14796 1302
2007 13068 1212
2008 13425 1020
2009 11327 840
2010 14796 701
2011 15012 625
2012 13542 550
1212/(1212+13068)
[1] 0.08487395
For what proportion of motor vehicle thefts in 2012 was an arrest made?
550/(550+13542)
[1] 0.03902924
Since there may still be open investigations for recent crimes, this could explain the trend we are seeing in the data. There could also be other factors at play, and this trend should be investigated further. However, since we don’t know when the arrests were actually made, our detective work in this area has reached a dead end.
Analyzing this data could be useful to the Chicago Police Department when deciding where to allocate resources. If they want to increase the number of arrests that are made for motor vehicle thefts, where should they focus their efforts?
We want to find the top five locations where motor vehicle thefts occur. If you create a table of the LocationDescription variable, it is unfortunately very hard to read since there are 78 different locations in the data set. By using the sort function, we can view this same table, but sorted by the number of observations in each category. In your R console, type:
sort(table(mvt$LocationDescription))
Which locations are the top five locations for motor vehicle thefts, excluding the “Other” category? You should select 5 of the following options.
sort(table(mvtWeek1$LocationDescription))
AIRPORT BUILDING NON-TERMINAL - SECURE AREA
1
AIRPORT EXTERIOR - SECURE AREA
1
ANIMAL HOSPITAL
1
APPLIANCE STORE
1
CTA TRAIN
1
JAIL / LOCK-UP FACILITY
1
NEWSSTAND
1
BRIDGE
2
COLLEGE/UNIVERSITY RESIDENCE HALL
2
CURRENCY EXCHANGE
2
BOWLING ALLEY
3
CLEANING STORE
3
MEDICAL/DENTAL OFFICE
3
ABANDONED BUILDING
4
AIRPORT BUILDING NON-TERMINAL - NON-SECURE AREA
4
BARBERSHOP
4
LAKEFRONT/WATERFRONT/RIVERBANK
4
LIBRARY
4
SAVINGS AND LOAN
4
AIRPORT TERMINAL UPPER LEVEL - NON-SECURE AREA
5
CHA APARTMENT
5
DAY CARE CENTER
5
FIRE STATION
5
FOREST PRESERVE
6
BANK
7
CONVENIENCE STORE
7
DRUG STORE
8
OTHER COMMERCIAL TRANSPORTATION
8
ATHLETIC CLUB
9
AIRPORT VENDING ESTABLISHMENT
10
AIRPORT PARKING LOT
11
SCHOOL, PRIVATE, BUILDING
14
TAVERN/LIQUOR STORE
14
FACTORY/MANUFACTURING BUILDING
16
BAR OR TAVERN
17
WAREHOUSE
17
MOVIE HOUSE/THEATER
18
RESIDENCE PORCH/HALLWAY
18
NURSING HOME/RETIREMENT HOME
21
TAXICAB
21
DEPARTMENT STORE
22
HIGHWAY/EXPRESSWAY
22
SCHOOL, PRIVATE, GROUNDS
23
VEHICLE-COMMERCIAL
23
AIRPORT EXTERIOR - NON-SECURE AREA
24
OTHER RAILROAD PROP / TRAIN DEPOT
28
SMALL RETAIL STORE
33
CONSTRUCTION SITE
35
CAR WASH
44
COLLEGE/UNIVERSITY GROUNDS
47
GOVERNMENT BUILDING/PROPERTY
48
RESTAURANT
49
CHURCH/SYNAGOGUE/PLACE OF WORSHIP
56
GROCERY FOOD STORE
80
HOSPITAL BUILDING/GROUNDS
101
SCHOOL, PUBLIC, BUILDING
114
HOTEL/MOTEL
124
COMMERCIAL / BUSINESS OFFICE
126
CTA GARAGE / OTHER PROPERTY
148
SPORTS ARENA/STADIUM
166
APARTMENT
184
SCHOOL, PUBLIC, GROUNDS
206
PARK PROPERTY
255
POLICE FACILITY/VEH PARKING LOT
266
AIRPORT/AIRCRAFT
363
CHA PARKING LOT/GROUNDS
405
SIDEWALK
462
VEHICLE NON-COMMERCIAL
817
VACANT LOT/LAND
985
RESIDENCE-GARAGE
1176
RESIDENCE
1302
RESIDENTIAL YARD (FRONT/BACK)
1536
DRIVEWAY - RESIDENTIAL
1675
GAS STATION
2111
ALLEY
2308
OTHER
4573
PARKING LOT/GARAGE(NON.RESID.)
14852
STREET
156564
Create a subset of your data, only taking observations for which the theft happened in one of these five locations, and call this new data set “Top5”. To do this, you can use the | symbol. In lecture, we used the & symbol to use two criteria to make a subset of the data. To only take observations that have a certain value in one variable or the other, the | character can be used in place of the & symbol. This is also called a logical “or” operation.
Alternately, you could create five different subsets, and then merge them together into one data frame using rbind.
How many observations are in Top5?
Top5 <- subset(mvtWeek1, LocationDescription=="STREET" | LocationDescription=="PARKING LOT/GARAGE(NON.RESID.)" | LocationDescription=="ALLEY" | LocationDescription=="GAS STATION" | LocationDescription=="DRIVEWAY - RESIDENTIAL")
str(Top5)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 177510 obs. of 13 variables:
$ ID : int 8951354 8951141 8952223 8951608 8950793 8950760 8951611 8951802 8950706 8951585 ...
$ Date : Date, format: "2012-12-31" "2012-12-31" "2012-12-31" ...
$ LocationDescription: chr "STREET" "STREET" "STREET" "STREET" ...
$ Arrest : logi FALSE FALSE FALSE FALSE TRUE FALSE ...
$ Domestic : logi FALSE FALSE FALSE FALSE FALSE FALSE ...
$ Beat : int 623 1213 724 211 2521 423 231 1021 1215 1011 ...
$ District : int 6 12 7 2 25 4 2 10 12 10 ...
$ CommunityArea : int 69 24 67 35 19 48 40 29 24 29 ...
$ Year : int 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ...
$ Latitude : num 41.8 41.9 41.8 41.8 41.9 ...
$ Longitude : num -87.6 -87.7 -87.7 -87.6 -87.8 ...
$ Month : chr "十二月" "十二月" "十二月" "十二月" ...
$ Weekday : chr "星期一" "星期一" "星期一" "星期一" ...
R will remember the other categories of the LocationDescription variable from the original dataset, so running table(Top5$LocationDescription) will have a lot of unnecessary output. To make our tables a bit nicer to read, we can refresh this factor variable. In your R console, type:
Top5$LocationDescription = factor(Top5$LocationDescription)
If you run the str or table function on Top5 now, you should see that LocationDescription now only has 5 values, as we expect.
Use the Top5 data frame to answer the remaining questions.
One of the locations has a much higher arrest rate than the other locations. Which is it? Please enter the text in exactly the same way as how it looks in the answer options for Problem 4.1.
table(Top5$LocationDescription, Top5$Arrest)
FALSE TRUE
ALLEY 2059 249
DRIVEWAY - RESIDENTIAL 1543 132
GAS STATION 1672 439
PARKING LOT/GARAGE(NON.RESID.) 13249 1603
STREET 144969 11595
On which day of the week do the most motor vehicle thefts at gas stations happen? (Monday~Sunday)
table(Top5$LocationDescription, Top5$Weekday)
星期一 星期二 星期三 星期五 星期六 星期日 星期四
ALLEY 320 323 317 385 341 307 315
DRIVEWAY - RESIDENTIAL 255 243 234 257 202 221 263
GAS STATION 280 270 273 332 338 336 282
PARKING LOT/GARAGE(NON.RESID.) 2128 2073 2103 2331 2199 1936 2082
STREET 22305 21888 22371 23773 22175 21756 22296
On which day of the week do the fewest motor vehicle thefts in residential driveways happen?(Monday~Sunday)
table(Top5$LocationDescription, Top5$Weekday)
星期一 星期二 星期三 星期五 星期六 星期日 星期四
ALLEY 320 323 317 385 341 307 315
DRIVEWAY - RESIDENTIAL 255 243 234 257 202 221 263
GAS STATION 280 270 273 332 338 336 282
PARKING LOT/GARAGE(NON.RESID.) 2128 2073 2103 2331 2199 1936 2082
STREET 22305 21888 22371 23773 22175 21756 22296