Chapter 1 R Setup

1.1 Preparing your environment

The Institute and Faculty of Actuaries have provided their own guide to getting up and running with R.

The steps to have R working is dependant on your operating system. Thankfully the following resources should allow for your local installation of R to be relatively painless:

  1. Download and install R from CRAN1.
  2. Download and install an integrated development environment, I recommend RStudio Desktop.

1.2 Basic interations with R

R prefers vectorised operations (over concepts like for loops)

# This is the syntax for comments in R
(1:10) + 2 # Notice how we add element-wise in R
##  [1]  3  4  5  6  7  8  9 10 11 12

We assign values to variables using the <- (“assignment”) operator2.

x <- 1:10
y <- x + 2
x <- x + x # Notice that we can re-assign values to variables
z <- x + 2
y
##  [1]  3  4  5  6  7  8  9 10 11 12
z
##  [1]  4  6  8 10 12 14 16 18 20 22

Even though \(z\) is assigned the same way as we assigned \(y\), note that \(y \neq z\) so execution order matters in R

We now add functions to the R code which has the form function_name(arguments = "values", ...)

# Combine function, used often to create vectors:
x <- c(1:3, 6:20, 21:42) 
# Another function with arguments:
y <- sample(x, size = 3)
y
## [1] 39 28 37

Let’s create a matrix in R

Note: Matrix multiplication requires the %*% syntax

first_matrix <- matrix(1:9, byrow = TRUE, nrow = 3)
first_matrix %*% first_matrix
##      [,1] [,2] [,3]
## [1,]   30   36   42
## [2,]   66   81   96
## [3,]  102  126  150

A data.frame is a very popular data structure used in R. Each input variable has to have the same length but can be of different types (strings, integers, booleans, etc.).

# Input vectors for the data.frame
name <- c("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
surface_gravity <- c(0.38, 0.904, 1, 0.3794, 2.528, 1.065, 0.886, 1.14)
# Create a data.frame from the vectors
solar_system <- data.frame(name, surface_gravity)
str(solar_system)
## 'data.frame':	8 obs. of  2 variables:
##  $ name           : chr  "Mercury" "Venus" "Earth" "Mars" ...
##  $ surface_gravity: num  0.38 0.904 1 0.379 2.528 ...

R has built in logic expressions:

Operator Description
< (<=) less than (or equal to)
> (>=) greater than (or equal to)
== exactly equal to
! NOT
& AND (element-wise)
| OR (element-wise)
!= not equal to

We can use logical expressions to effectively filter data

Here we subset the data using the [...] syntax

x <- 1:10
x[x != 5 & x < 7]
## [1] 1 2 3 4 6

We can select objects using the $ symbol - see ?Extract for more help here

#data.frame[rows to select, columns to select]
solar_system[solar_system$name == "Jupiter", c(1:2)]
##      name surface_gravity
## 5 Jupiter           2.528

We can extend R’s functionality by loading packages:

# Load the ggplot2 package
library(ggplot2)
  • Did you get an error from R trying this?
  • To load packages they need to be installed first:
  • install.packages("ggplot2")

  1. CRAN is the The Comprehensive R Archive Network - read more on the CRAN website↩︎

  2. We can also assign values using the more familiar = symbol. In general this is discouraged, listen to Hadley Wickham.↩︎