Skip to main content

Section 1.5 Getting started with crime data

We live in a world awash with data and through this book we will use the different examples to show you some useful places and sources you can use to obtain your spatial data. Most geographically referenced crime data is based on crime reported to the police, although there are some notable exceptions (e.g., public health data on violence, geocoded victim survey data, etc.). Many police departments across the world make this data readily available to the public or researchers. This is more common in places like the United States, where crime mapping applications first got established, but increasingly we see publicly available crime data from other countries as well.
For our first map we will use data from the UK, which can be downloaded from the police.uk website. We have downloaded some data for crime in Manchester, which is included in the data file you have downloaded to follow along (see the Preamble chapter for details). If you wanted to acquire the data yourself directly from the source, you could open the data.police.uk/data website and then choose the data tab, in order to manually download some data.
So whether from the website, or the provided data file, you should have some police data ready. The next step is to read this into R. How can we do this? We say, "hello R, i would like to create a new object please and I will call this new object my_data." We do this by typing the name we are giving the object and the assignment operator <-. Then on the right, hand side of the assignment operator, there is the value that we are assigning the variable. So it could be a bit of text, or it could be some function, for example when you read a csv file with the read_csv() function from the readr package.
When we read in this file, inside the function, we also need to specify where to read the csv from. Where should R look to find this data? This is where normally you are putting in the path to your file. Something like:
my_data <- read_csv("path to my file here")
If you downloaded the data following the steps in the Preface chapter for this book, your data will be in your working directory, in a sub-folder called "data". It is within this sub-folder that you’ll find all you need. In this case, let’s look for the file 2019-06-greater-manchester-street.csv. To read this into R we run:
library(readr)

crimes <- read_csv("data/2019-06-greater-manchester-street.csv")
If you look at the Environment window in the top right corner of RStudio you should see now a new object that contains a tibble, a particular format for dataframes, and that is called crimes. It will tell you how many observations (rows - and incidentally the number of recorded crimes in June 2019 within the GMP jurisdiction) and how many variables (columns) your data has. Let’s have a look at the crimes dataframe with the View() function. This will open the data browser in RStudio.
View(crimes)
If you just want your results in the console, you can use the glimpse() function from the tibble package. This function does just that: it gives you a quick glimpse of the first few cases in the dataframe. Notice that there are two columns (Longitude and Latitude) that provide the required geographical coordinates that we need to plot this data.
library(tibble)

glimpse(crimes)
## Rows: 32,058
## Columns: 12
## $ `Crime ID`              <chr> NA, "aa1cc4cb0c436f46...
## $ Month                   <chr> "2019-06", "2019-06",...
## $ `Reported by`           <chr> "Greater Manchester P...
## $ `Falls within`          <chr> "Greater Manchester P...
## $ Longitude               <dbl> -2.464, -2.441, -2.44...
## $ Latitude                <dbl> 53.61, 53.62, 53.61, ...
## $ Location                <chr> "On or near Parking A...
## $ `LSOA code`             <chr> "E01004768", "E010047...
## $ `LSOA name`             <chr> "Bolton 001A", "Bolto...
## $ `Crime type`            <chr> "Anti-social behaviou...
## $ `Last outcome category` <chr> NA, "Unable to prosec...
## $ Context                 <lgl> NA, NA, NA, NA, NA, N...
You may notice that a lot of the variable names are messy in that they have a space in them, this can cause issues, so before playing around too much with the data we want to clean this up. Luckily there is a very handy package you can use for this called janitor which contains the function clean_names(). This function will clean your variable names not only of spaces but also of special characters, and it will convert characters to lower-case, to follow tidyverse naming conventions outlined in the tidyverse style guide [241].
library(janitor)

crimes <- clean_names(crimes)
Now the names are much neater. You can print them all for a view using the names() function:
names(crimes)
##  [1] "crime_id"              "month"
##  [3] "reported_by"           "falls_within"
##  [5] "longitude"             "latitude"
##  [7] "location"              "lsoa_code"
##  [9] "lsoa_name"             "crime_type"
## [11] "last_outcome_category" "context"