This brief document will guide you through the basics needed to set up R for the “Advanced Crime Analysis” module.

## What’s in a name?

You will notice that we use the terms R notebook, R, and R studio. They do sound similar and are all closely related but it’s good to grasp what each of these is.

• R notebook refers to an interactive document like this current one, which you can open as an .html file in your browser or as a .Rmd (= R markdown format) in R. You can run code directly from that file in R Studio. We will use R notebooks for the tutorials and you will submit the code for your capstone project in an R notebook, too.
• R Studio is the software that allows you to run R in a nice interface with different panes on the screen (e.g. the source of the code, the console with the output, a plot pane for the graphics display, etc.)
• R is the programming language that we use in this module. It shares many similarities with other languages such as python but is more suitable for statistics and data science (my biased opinion!).

We strongly encourage you to bring your own laptop to the tutorials. You will be able to run R Studio on it and do all the necessary work with it. If you do not have your own laptop, you will be able to use UCL’s computers in the cluster room.

### Installing R Studio

R Studio is the software that we will sue to write, develop and run R commands.

Please follow this guide here to install R Studio Desktop (and the required core R) on your machine.

Update: the link to “R for Windows” seems broken, please use this one if you’re on windows: https://cran.r-project.org/bin/windows/base/.

## Customising R Studio

There are two important ways in which you can (and should) customise R Studio.

1. Changing the appearance of R Studio

A brief guide to setting up the workspace to your own preference is this one. Please go through these steps and make R Studio your own as you want it.

1. Installing dependencies/libraries

R has an extensive collection of ‘core’ functions that cover most of the statistical analyses you will encounter.

An example is calculating a simple average:

# This creates a variable "a" with five numerical values
a = c(1, 2, 3, 4, 5)
print(a)
## [1] 1 2 3 4 5
#We can use the core functions of R to calculate the average (mean)
mean(a)
## [1] 3

However: in many cases you will have to use so-called libraries/packages that provide additional - often more advanced or more specialised - functionalities on top of the core R.

In essence, these packages add new functions to R.

An example is the rvest package that you saw in use for the web-scraping example in the introduction lecture.

Each package consists of: - a pdf manual that documents the functions of the package - here’s the rvest pdf - an openly available code base - the source code of rvest - an integrated R help: each function of installed packages is documented in R itself and can be retrieved with the ? command: try ?mean

To install a library, you can use the install.packages() command (details on Jeffrey Leek’s slides here) or by clicking on Tools –> Install packages –> “typing the package name”.

Use one of these steps to install the rvest package.

To “activate” the package, you need to load it for your R session using the library command:

library(rvest)
## Loading required package: xml2

To test whether all of the above has worked, try to run code the code shown in the lecture to access the FBI’s most wanted terrorists website.

# Note: we will go into depth with this code next week. For now, it suffices if you run the code
target_page %>%
html_nodes('p.name') %>%
html_text()
##  [1] "SHAYKH AMINULLAH"
##  [2] "FAKER BEN ABDELAZZIZ BOUSSORA"
##  [3] "ABDULLAH AL-RIMI"
##  [4] "IBRAHIM SALIH MOHAMMED AL-YACOUB"
##  [6] "ABDELKARIM HUSSEIN MOHAMED AL-NASSER"
##  [7] "ALI ATWA"
##  [8] "ABDUL RAHMAN YASIN"
## [10] "ALI SAED BIN ALI EL-HOORIE"
## [11] "ABD AL AZIZ AWDA"
## [13] "JABER A. ELBANEH"
## [14] "JAMEL AHMED MOHAMMED ALI AL-BADAWI"
## [16] "AYMAN AL-ZAWAHIRI"
## [18] "ADNAN G. EL SHUKRIJUMAH"
## [19] "ABDERRAOUF JDEY"
## [22] "LIBAN HAJI MOHAMED"
## [23] "LEO FREDERICK BURT"
## [24] "ISHMAIL MUSLIM ALI"
## [25] "JOSE ESPINOSA CABALLERO"
## [26] "EDUARDO GUERRA JIMENEZ"
## [27] "AMBROSE HENRY MONTFORT"
## [28] "JEAN-PIERRE CHARETTE"
## [29] "ALAIN ALLARD"
## [30] "HASAN IZZ-AL-DIN"
## [31] "SIRAJUDDIN HAQQANI"
## [32] "AMER EL-MAATI"
## [33] "GEORGE EDWARD WRIGHT"
## [40] "GHAZI NASR AL-DIN"