In data/mvtWeek1.csv there are vehicle stolen event records in Chicago city. Below are the definitions of the columns …

The first question is, and should always be, what can we do with this data?

While we conduct the practical analysis, we’ll also …



Section-1 Loading the Data

Read the data into a data frame D.

D = read.csv("data/mvtWeek1.csv")

As I said, the names of your major data frame is the shorter the better.

【1.1】How many rows of data (observations) are in this dataset?

#

【1.2】How many variables are in this dataset?

#

Let’s also check the data types of these variables

#

and do a quick summary on them.

#

【1.3】Using the “max” function, what is the maximum value of the variable ID?

#

【1.4】 What is the minimum value of the variable Beat?

#

【1.5】 How many observations have value TRUE in the Arrest variable (this is the number of crimes for which an arrest was made)?

sum(D$Arrest)
[1] 15536

What is the average arrest rate of car thief events?

#

【1.6】 How many observations have a LocationDescription value of ALLEY?

#



Section-2 Processing Date and Time in R

【2.1】 In what format are the entries in the variable Date?

head(D$Date)  # Month/Day/Year Hour:Minute
[1] "12/31/12 23:15" "12/31/12 22:00" "12/31/12 22:00" "12/31/12 22:00" "12/31/12 21:30"
[6] "12/31/12 20:30"

🌞 Date and Time are quite troublesome. Naturally the time line is a rational linear dimension. However, these nice real-number properties are all messed up in human society. Months have different days. Week may span over two months. The worst is, people record date and time in so many different formats that computers cannot read them automatically. Therefore, dates and time are usually read in as character strings. We have to manually look into the strings, recognize the format and then convert them into Date or POSIXct (name of a standard timing system) data types before we can properly using them in our programs. For an example, we need to observe the format in D$date and convert this chr column into a POSIXct vector (ts).

ts = as.POSIXct(D$Date, format="%m/%d/%y %H:%M")

🌻 as.POSIXct(x, format) converts a string vector x into a POSIXct vector.

🌷 Watch how format of the time string is specified in Date and Time Codes (see Table-1 below)

Date and Time Formating Codes
Table-1 Date and Time Formatting Codes

Now we can make histograms of this time variable (ts) in the same way as we do for numerics …

par(mfrow=c(1,2), mar=c(4,3,3,1), cex=0.7)
hist(ts,"year",las=2,freq=T,xlab="",main="Event Counts in Years")
hist(ts,"quarter",las=2,freq=T,xlab="",main="Event Counts in Quartera")



🌻 format() can convert date/times back to strings in the desirable format

For examples, if we want to count the car thief events by weekdays …

table( format(ts,'%w') )

    0     1     2     3     4     5     6 
26316 27397 26791 27416 27319 29284 27118 

by hours …

table( format(ts,'%H') )

   00    01    02    03    04    05    06    07    08    09    10    11    12    13    14 
13212  6643  5377  4202  3357  3728  5136  6852  7866  8136  6566  5415  8158  5555  6419 
   15    16    17    18    19    20    21    22    23 
 7512  7736  8635 10521 10427 11716 12434 14745 11293 

by 7 days and 24 hours …

table(weekday=format(ts,'%u'), month=format(ts,'%H'))
       month
weekday   00   01   02   03   04   05   06   07   08   09   10   11   12   13   14   15
      1 1900  825  712  527  415  542  772 1123 1323 1235  971  737 1129  824  958 1059
      2 1691  777  603  464  414  520  845 1118 1175 1174  948  786 1108  762  908 1071
      3 1814  790  619  469  396  561  862 1140 1329 1237  947  763 1225  804  863 1075
      4 1856  816  696  508  400  534  799 1135 1298 1301  932  731 1093  752  831 1044
      5 1873  932  743  560  473  602  839 1203 1268 1286  938  822 1207  857  937 1140
      6 2050 1267  985  836  652  508  541  650  858 1039  946  789 1204  767  963 1086
      7 2028 1236 1019  838  607  461  478  483  615  864  884  787 1192  789  959 1037
       month
weekday   16   17   18   19   20   21   22   23
      1 1136 1252 1518 1503 1622 1815 2009 1490
      2 1090 1274 1553 1496 1696 1816 2044 1458
      3 1076 1289 1580 1507 1718 1748 2093 1511
      4 1131 1258 1510 1537 1668 1776 2134 1579
      5 1165 1318 1623 1652 1736 1881 2308 1921
      6 1055 1084 1348 1390 1570 1702 2078 1750
      7 1083 1160 1389 1342 1706 1696 2079 1584

With a few more line of codes we can transform the table into a heatmap that illustrates the time distribution of car thief events nicely. The plotting function will be elaborated latter in this course

table(format(ts,"%u"), format(ts,"%H")) %>% 
  as.data.frame() %>% setNames(c("weekday","hour","count")) %>% 
  ggplot(aes(hour,weekday,fill=count)) +
  geom_tile() + coord_fixed(ratio=1) + 
  scale_fill_gradientn(colors=c("seagreen","yellow","darkred"))


💡 Hints: The most useful functions in R are …
  ■ table()
  ■ tapply()
incorporating with …
  ■ sort(), head(), tail()
  ■ sum(), mean, max(), min
You should be able to solve all fo the questions below


【2.2】 What is the month and year of the median date in our dataset?

#

【2.3】 In which month did the fewest motor vehicle thefts occur?

# 

【2.4】 On which weekday did the most motor vehicle thefts occur?

# 

【2.5】 Which month has the largest number of motor vehicle thefts for which an arrest was made?

#