#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 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.
Click on Session > Set working directory > Choose directory…
Then navigate to the folder you want to use, open it, and click on ‘Open’.
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.
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.
Date range
just select June 2016 - June 2016Force
find Greater Manchester Police
, and tick the box next to it.Data sets
tick Include crime data
.Generate File
button.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.
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.