logo #Intro day R data tasks

To get comfortable with using R for playing around with data, let’s get some actual crime data, and have a look at it.

Create a folder to work in

Create a folder to save your data and our outputs in. In R, this is known as a working directory. You can name this anything. In this case, I created a folder called ‘demo’ inside the folder that has the course notes. I now have to tell R about this folder. You can do this a few ways.

One way is simple point and click:

Click on Session > Set working directory > Choose directory…

Set working directory

Then navigate to the folder you want to use, open it, and click on ‘Open’.

Or the other way is to use code (you need to know the filepath for this):

The command to set working directory is setwd(). Inside the brackets you need to write the path to your folder, in quotation marks. So for me this is:

setwd("~/Desktop/R-for-Criminologists/demo")

Whichever way you choose, once you have done this you can save all data in this folder, and read them in from here. Also any outputs like plots and code get saved here as well.

Get some data

We can play around with police recorded crime data, which can be downloaded from the police.uk website.

Let’s stick local and download some data for crime in Manchester.

To do this, open the data.police.uk/data website.

This will take you to a download page, where you have to click the Download now button. This will open a dialogue to save a .zip file. Navigate to the working directory folder you’ve readet and save it there. Unzip the file.

Read the data into R

R reads in data in the form of dataframes. This is essentially the same as a spreadsheet. You have columnds that represent your variables. And rows that reporesent your observations

So for example, in this data set each row is one crime record.