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.

Getting your machine ready for R

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 (and the required core R) on your machine.

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 = read_html('https://www.fbi.gov/wanted/terrorism')
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"            
##  [5] "RAMADAN ABDULLAH MOHAMMAD SHALLAH"           
##  [6] "ABDELKARIM HUSSEIN MOHAMED AL-NASSER"        
##  [7] "ALI ATWA"                                    
##  [8] "ABDUL RAHMAN YASIN"                          
##  [9] "HUSAYN MUHAMMAD AL-UMARI"                    
## [10] "ALI SAED BIN ALI EL-HOORIE"                  
## [11] "ABD AL AZIZ AWDA"                            
## [12] "AHMAD IBRAHIM AL-MUGHASSIL"                  
## [13] "JABER A. ELBANEH"                            
## [14] "JAMEL AHMED MOHAMMED ALI AL-BADAWI"          
## [15] "MOHAMMED ALI HAMADEI"                        
## [16] "AYMAN AL-ZAWAHIRI"                           
## [17] "AHMAD ABOUSAMRA"                             
## [18] "ADNAN G. EL SHUKRIJUMAH"                     
## [19] "ABDERRAOUF JDEY"                             
## [20] "RADDULAN SAHIRON"                            
## [21] "JEHAD SERWAN MOSTAFA"                        
## [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"                        
## [34] "MUHAMMAD AHMED AL-MUNAWAR"                   
## [35] "MUHAMMAD ABDULLAH KHALIL HUSSAIN AR-RAHAYYAL"
## [36] "WADOUD MUHAMMAD HAFIZ AL-TURKI"              
## [37] "JAMAL SAEED ABDUL RAHIM"                     
## [38] "ABDULLAH AHMED ABDULLAH"                     
## [39] "SAIF AL-ADEL"                                
## [40] "GHAZI NASR AL-DIN"

In the upcoming tutorial session, you will learn how to navigate the R environment and how to solve data science problems.

END