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:
- Download and install
R
from CRAN1. - 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.
1:10
x <- x + 2
y <- x + x # Notice that we can re-assign values to variables
x <- x + 2
z <-
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≠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:
c(1:3, 6:20, 21:42)
x <-# Another function with arguments:
sample(x, size = 3)
y <- y
## [1] 39 28 37
Let’s create a matrix in R
Note: Matrix multiplication requires the %*%
syntax
matrix(1:9, byrow = TRUE, nrow = 3)
first_matrix <-%*% 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
c("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
name <- c(0.38, 0.904, 1, 0.3794, 2.528, 1.065, 0.886, 1.14)
surface_gravity <-# Create a data.frame from the vectors
data.frame(name, surface_gravity)
solar_system <-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
1:10
x <-!= 5 & x < 7] x[x
## [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]
$name == "Jupiter", c(1:2)] solar_system[solar_system
## 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")
CRAN is the The Comprehensive R Archive Network - read more on the CRAN website↩︎
We can also assign values using the more familiar
=
symbol. In general this is discouraged, listen to Hadley Wickham.↩︎