R Introduction 1 (Getting Started)
R Introduction 1 (Getting Started)
Getting started Page 1
Getting started
Covered in R1
Installing R and RStudio
Working with R and RStudio
The Actuarial Education Company © IFE: 2019 Examinations
Page 2 R1: Getting started
1 Installing R
R is an open‐source programming language that is increasingly being used in the actuarial
profession with many applications in the world of statistics. If you haven’t used R before then
your first job is probably to install it on your computer or device. This section gives you a rough
guide on how to download and install R, although you may find that the process is slightly
different depending on the device you are using. You may also need to ask someone with
administrative privileges for help (eg your IT Service Desk) if you are installing R on a work
computer.
If you encounter any problems, then please do not contact ActEd for help. You will probably find
a solution much more quickly if you search the internet. Many people have published installation
guides as well as numerous problem solving tips in discussion forums.
But here’s our guide:
1. Visit http://www.stats.bris.ac.uk/R/. (If you are IT savvy then you can probably find the
relevant file to download and install R without following the instructions below.)
2. Click on “Download R for Windows” (assuming you are using a Windows‐based system).
3. Click on “base”.
4. Click on “Download R 3.5.1 for Windows” or whatever the latest version is.
© IFE: 2019 Examinations The Actuarial Education Company
R1: Getting started Page 3
5. Next you will probably
need to “Save File”:
6. Find the downloaded file (eg R‐3.3.0‐win.exe) where it was saved and double click on it.
7. You may then have a security
warning which you’ll need to
dismiss with “Run”.
8. You can quickly progress through the next four pop‐ups with one click of OK and three
clicks of Next. However, please note the information in the third popup about
administrator rights.
The Actuarial Education Company © IFE: 2019 Examinations
Page 4 R1: Getting started
9. Choose where you want to install R (or use the default
location) and click Next. If you are using a work computer,
it may be easiest to use a folder in your personal area, for
example in My Documents.
10. Select the installation type appropriate for
your device.
11. Click No and then Next to select the defaults.
© IFE: 2019 Examinations The Actuarial Education Company
R1: Getting started Page 5
12. A couple more clicks of Next (after changing the options if you wish) and then Finish and
you’re done.
13. You should then be able to run R using the desktop icon or via the Start menu.
We will look at how to use R later.
The Actuarial Education Company © IFE: 2019 Examinations
Page 6 R1: Getting started
2 Installing RStudio
We will have a very brief look at working directly in R in the next section, but most of the time we
will instead be working in RStudio. This is a more user‐friendly interface which you will probably
find easier to use. So you now need to install this as well:
1. Visit https://www.rstudio.com/products/rstudio/download/#download and download the
relevant open source RStudio Desktop for your operating system.
2. Run the .exe file and follow the installation instructions. (You may need to ask your IT
service desk for help if you need administrator privileges.)
3. If you run into any difficulties then we recommend that you browse the internet for help.
© IFE: 2019 Examinations The Actuarial Education Company
R1: Getting started Page 7
3 Working directly in R
Although we will be working most of the time in RStudio, it might be useful to have a quick look at
R itself.
When you load up R you’ll be greeted with R’s graphical user interface (or GUI for short).
Inside this you’ll see one open window which is called the R console.
Everything interesting happens in this window.
R console
You can change the display preferences by Edit/GUI preferences, where you can change features
like the font, size and style (normal, bold, italic) of the text:
The Actuarial Education Company © IFE: 2019 Examinations
Page 8 R1: Getting started
Clearing the screen
We can clear the console window either by typing
CTRL+L (or choosing clear console from the Edit
menu):
Entering commands
Unlike modern mouse driven programs, R is a command based programming language.
So rather than choosing options from menus or clicking on icons we’ll be typing commands into
the console window that tell R what we want it to do.
We’ll then execute those commands by pressing enter.
R will return the results of our instruction in this console window.
For example if we type 2+3 and then press enter we’ll get
5, as shown.
In this introduction, we will write the command you will enter in red and the results of executing
that command in blue.
So for the above we would have written:
2+3
5
The trouble with R is going to be remembering the names of the commands, which is made
harder by the fact that R is case sensitive…
We’ll cover the essential commands later.
© IFE: 2019 Examinations The Actuarial Education Company
R1: Getting started Page 9
Graphics window
If we produce any graphics then they will appear in a separate window to the console, called the
graphics window.
R console
Graphics window
To see for yourself type:
demo(graphics)
You’ll need to hit enter each time to move onto the next graphic.
Use the standard windows icons to maximise, minimise or close the
graphics window.
The Actuarial Education Company © IFE: 2019 Examinations
Page 10 R1: Getting started
Script window
Rather than entering commands directly in the console window we can use another window
called the script window (or script editor).
Script window
You can open this window first by clicking in the Console (to get the right menus at the top of the
GUI), and then choosing New script from the File menu:
Just like a script for a play or movie which contains the lines that you read out – it has the lines of
commands which can be “read out” or put into the console window either using copy and paste,
or more quickly by clicking on the line and typing CTRL+R. We’ll talk about scripts more later on.
© IFE: 2019 Examinations The Actuarial Education Company
R1: Getting started Page 11
Other interfaces
This is the basic graphics user interface (GUI) for R. As already mentioned, other packages are
also available which offer user‐friendly features, for example RStudio, which we will be using
shortly. Another example is R commander which is another GUI which expands the menus to
include standard commands such as importing data, producing graphs, carrying out tests and
fitting models to the data set.
Quitting R
To end your session in R you could type the command quit( ) or just q( ) in the console.
Alternatively choose exit from the File menu or just click on the close window icon in the corner:
R will then ask you if you want to save the workspace image:
We’ll talk about workspaces more later. Suffice to say, if you have created any objects (that is
important things assigned a special name) that you want next time then you may wish to click on
yes. If you haven’t done anything you wish to save, the just click No.
The Actuarial Education Company © IFE: 2019 Examinations
Page 12 R1: Getting started
4 Working in RStudio
Now you have had a quick look at R, it won’t take long to become familiar with the basics of
RStudio.
Start/run RStudio and you will probably see something like this:
The panel on the left hand side is simply R’s Console, which we have already met. The panel at
the top on the right has a number of tabs. The first is the Environment (or Workspace) which will
prove very useful as it displays the values of variables and contents of datasets that we are using.
The second tab, History, not surprisingly displays a history of your work in R. We won’t worry
about the third tab for now.
The panel at the bottom on the right also has a number of useful tabs. One displays recently used
files, allowing you to access them quickly. Another, Plots, is simply the graphics window and will
display the plots/graphs that you ask R to produce. There are also important tabs called Packages
and Help which we will look at later.
If you open up a Script in RStudio, using File, New File, R Script, or by clicking on
the drop‐down arrow and then R Script (found in the top right‐hand corner of
the screen), then RStudio will display all four panels neatly arranged.
© IFE: 2019 Examinations The Actuarial Education Company
R1: Getting started Page 13
You can drag the edges of the panels to size them as you wish:
Commands
Just like in R, to clear the Console press Ctrl L from within the prompt of the Console.
To run lines of codes from the Script window, press Ctrl Enter (and not Ctrl R). Alternatively use
the Run Button on the top bar of the Script window.
Quitting RStudio
You can exit RStudio in the same way you exit R, or just press Ctrl Q.
The Actuarial Education Company © IFE: 2019 Examinations
Page 14 R1: Getting started
5 Summary
Key terms
GUI Graphics User Interface
The name given to the appearance of the R program on your computer.
Console window The window where commands are entered and then executed by hitting
the enter key.
Graphics window The window where graphics are displayed. You can then export these to
put in any documentation you produce.
Script window A window where commands can be written but not executed. We can
transfer them to the console window and execute them using CTRL+R.
This will be covered in a later chapter.
Menus
R RStudio
Key commands
R RStudio
© IFE: 2019 Examinations The Actuarial Education Company
R1: Getting started Page 15
6 Have a go
You will only get proficient at R by practising.
Try the following in R or RStudio:
Start a new session
Clear the console screen
Use R to calculate 3+5 (or something more daring)
Quit R using a command, not the menu or windows icons.
The Actuarial Education Company © IFE: 2019 Examinations
All study material produced by ActEd is copyright and is sold
for the exclusive use of the purchaser. The copyright is
owned by Institute and Faculty Education Limited, a
subsidiary of the Institute and Faculty of Actuaries.
Unless prior authority is granted by ActEd, you may not hire
out, lend, give out, sell, store or transmit electronically or
photocopy any part of the study material.
You must take care of your study material to ensure that it
is not used or copied by anybody else.
Legal action will be taken if these terms are infringed. In
addition, we may seek to take disciplinary action through
the profession or through your employer.
These conditions remain in force after you have finished
using the course.
© IFE: 2019 Examinations The Actuarial Education Company