R and RStudio Tutorial for ECON20003
R and RStudio Tutorial for ECON20003
Semester 2 - 2023
TUTORIAL 1
Read this handout, watch the corresponding R video, and complete the tutorial exercises before
your tutorial class so that you can ask for help during the tutorial if necessary.
After the tutorial class attempt the “Exercises for assessment”. For each assessment exercise
type your answer in the corresponding box available in the Quiz. If the exercise requires you to
use R, insert the relevant R/RStudio script and printout in the same Quiz box below your
answer. To get the tutorial mark on week 2, you must submit your answers to these exercises
in the Tutorial 1 Canvas Quiz by 10am Wednesday in Week 2 and attend Tutorial 2.
In QM2 tutorials some of the calculations will be performed manually (i.e., with a Casio fx-82
or some similar calculator) to get a feel for statistics and to help you understand the various
statistical procedures, but the emphasis will be on learning how to use two free and open-
source software programs.
The first program is R, which was originally created by Ross Ihaka and Robert Gentleman in
the 1990s at the University of Auckland, New Zealand, and is currently developed by the R
Development Core Team. It is a programming language and environment for statistical
computing and data visualization. The second program is RStudio, an integrated development
environment (IDE) for R. Among others, it comprises the R environment, an advanced text
editor, and the help system of R. RStudio itself does not perform any statistical operations, they
are performed by R in the background, and R can be perfectly used without it. However,
RStudio makes working with R much easier as it makes more convenient to set up a working
directory and access files on the computer, to write and execute R codes, and to view and use
the various available R objects.
R has a text-based interface where one can enter R commands. These commands are
processed by R and the results (if any) are printed on the screen. This interface is not as
elegant and convenient than the point-and-click interface of popular proprietary statistical /
econometric packages, like e.g., that of EViews, making it relatively hard to learn how to use
R. Although RStudio certainly flattens the learning curve, you might still wonder why to bother
to use R and RStudio at all, why not to use instead some more stylish program like EViews, or
even Microsoft Excel that most students already have on their computers and are familiar with.
1
L. Kónya ECON20003 - Tutorial 1
The main advantage of R and RStudio compared to EViews, and similar commercial packages
is that anybody can download and install them on his/her computer free of charge. This way
students can work on the tutorial exercises and assignment questions out off campus any time.
As for Excel and similar spreadsheet programs, they do not naturally lend themselves to
‘serious’ data analysis. They can be convenient for basic statistics but using spreadsheets for
more complex calculations is cumbersome and generally a bad idea in the long run. By striving
to do so you would dig yourselves into a very deep hole.
R is an open-source program. On the one hand, this is a disadvantage because it means that
there isn’t a single company that would look after the maintenance and development of R. That
is why R is not as neat as commercial packages. On the other hand, it is an advantage because
it makes R highly extensible. Being an open-source program, R has a vast community both in
academia and in business who create and maintain literally thousands of well-documented
extension packages for a wide variety of statistical and graphical techniques greatly extending
the base functionality of R. These packages are available free, they can be downloaded from
a worldwide repository system, called the Comprehensive R Archive Network (CRAN). In
January 2022, it featured more than 18600 contributed packages. 1
You are recommended to read the tutorial handout and do the tutorial exercises on your own
prior to the tutorials. To be able to do so, you need to install R and RStudio on your own
computer.
To download R onto your computer, visit the Comprehensive R Archive Network (CRAN)
website ([Link] and select the download link per your operating system.
If, for example, you use Windows, select Download R for Windows. Then, select ‘install R for
the first time’ to download the latest version of R. At the time of writing this tutorial handout in
1
To get some idea about the ever-expanding library of R extension packages, you might wish to visit the following
site: [Link]
2
ECON20003 - Tutorial 1
early January 2022, the latest version is R-4.1.2. 2
On the new screen page (see below), click ‘Download R 4.1.2 for Windows’, choose ‘Save
File’, and once the [Link] file is downloaded, double-click on it to install R. Follow the
instructions, there is no need to change the default installation parameters.
Although R is a fully functional standalone program, RStudio can assist in writing, compiling,
debugging and executing R codes. For this reason, it is a good idea to have RStudio too on
your computer.
2
This semester all tutorial handouts are based on R-4.1.2. If in the meantime R gets updated, you might install
the latest version on your computer, just be aware that certain screenshots might be slightly different and
occasionally you might get some warning messages.
3
Note that if you are on a 32-bit system, you need to download and install some older version of RStudio.
4
Make sure to have R on your computer before installing RStudio.
3
ECON20003 - Tutorial 1
The ECON20003 tutorial handouts are prepared on a Windows computer, like the ones in the
FBE computer labs. R and RStudio are available for Mac computers as well and apart from
some minor differences they run the same way as on Windows computers.
You might wish to check out this video to see how to download and install R and RStudio on
your Mac computer:
[Link]
When both R and RStudio are installed on a computer, R can be used either outside or inside
RStudio.
Although for most users it is more convenient to use R from within RStudio, for the sake of
illustration, launch R first from the Start menu or by clicking on its shortcut on the desktop, to
check whether it has been installed properly. A window, like the one shown on the next page,
should appear on your screen. It shows the RGui (R Graphical user interface) window with the
R Console in it, a panel in which you can type R commands, submit them for execution and
view the results.
4
ECON20003 - Tutorial 1
As you can see in the last paragraph in the R Console, you can access some demos or get
some help by typing demo(), help() or [Link]() behind the red > symbol. Do not worry about
them at this stage, just type q() and press Enter to quit R.
In return, R displays a dialog box asking you whether to save the workspace image.
5
ECON20003 - Tutorial 1
The workspace is your current R working environment. It is a snapshot of your work to the point
of saving it and it includes all objects that you created during the current session or have loaded
from a previous session.
If you click the ‘Yes’ button, R saves an image of your workspace and reloads it automatically
the next time you start R. This is often a convenient option, especially if you have not completed
your project yet, just suspend it. However, even in this case it is probably better to save the
objects you intend to keep time after time during every session and not to wait till you quit R.
This time you do not have any object in your workspace yet, so just click the ‘No’ button.
Now launch RStudio from the Start menu or by clicking on its shortcut on the desktop and wait
for its window to appear. 5
The Main Menu, just below the Title Bar, is a set of drop-down menus titled File, Edit, Code,
View, Plots, Session, Build, Debug, Profile, Tools, and Help. These (sub-)menus can be used
the same way than drop-down menus in general in other Windows programs.
5
Do not worry if some of the details on your screen are not exactly the same than on the screenshot below. You
probably have different Global Options.
6
ECON20003 - Tutorial 1
When the program is launched the first time on a computer, under the Title Bar and the Main
Menu, RStudio displays three panels or windows: the Console/Terminal/Jobs panel (left), the
Environment/History/Connections panel (top-right), and the Files/Plots/Packages/Help/Viewer
panel (bottom-right).
RStudio has a fourth panel as well, but at this stage it is hidden by default. To open it, click the
File drop-down menu, choose New File / R Script. As the screenshot on the next page
illustrates, you should now have two panels on the left half of your screen: the new Source
panel (top-left) and the Console/Terminal/Jobs panel (bottom-left).
The Source panel (top-left) serves as a built-in text editor that allows you to create a new R
script or to open a file containing an existing R script. An R script, in general, is just a text file
with the R extension that keeps a record of your R code. By default, every new script created
by RStudio is Untitled. If you save it, your code will be available when you re-open RStudio.
The Console/Terminal/Jobs panel (bottom-left) is for entering proper R commands that execute
immediately and for viewing the output (Console tab), for providing access to the system shell
directly from the RStudio IDE (Terminal tab), and for running R scripts in batch mode in the
background while the user is working on a separate R session interactively (Jobs tab). 6
The Environment/History/Connections panel (top-right) shows the list of R objects (i.e., data
frames, arrays, values and functions) you have created in the Console during your R session
(Environment tab), the history of all previous commands (History tab), and all the existing and
currently active connections to supported data sources (Connections tab).
Before you begin working in RStudio, a working directory must be set up. It is just a folder, the
default location for all project files (input data-sets, plots and other objects) read into R and
saved out of R.
You should have your working directory at a convenient place where you can easily find it. Once
you have set up a folder there, you can set the working directory by following the Session / Set
Working Directory / Choose Directory… menu steps and navigating to your folder (see the first
screenshot on the next page). 7
6
In batch mode, a series of commands are run to completion without manual intervention, while in interactive
mode the user enters an instruction into the command line, the instruction is executed, the result is displayed on-
screen, and then the user is prompted to enter the next command.
7
Alternatively, the working directory can be set by executing the setwd(“file location") command.
7
ECON20003 - Tutorial 1
8
ECON20003 - Tutorial 1
My working directory is at > F: > Dady > Teaching > Quantitative Methods 2 and after having
selected it, RStudio executes the
command and echoes it in the Console (see the second screenshot on the previous page).
To check the current working directory, have a look at the Files tab. As you can see below, my
working directory is indeed at > F: > Dady > Teaching > Quantitative Methods 2.
getwd()
9
ECON20003 - Tutorial 1
The current working directory applies only for the current RStudio session. In order to make it
permanent, click on Tools on the main menu bar and select Global Options.
In the opening Options dialog window (see the screenshot on the next page) on the General
tab enter the address of your preferred directory in the Default working directory (when not in
project) box and then click on the OK button.
10
ECON20003 - Tutorial 1
Next, click the Help tab in the bottom-right panel to open the Help Home page.
11
ECON20003 - Tutorial 1
It provides links to extensive online help both for R and RStudio. Look at some of the options,
for example, the RStudio Cheat Sheets and the Search Engine & Keywords links.
Finally, quit RStudio by following the File / Quit Session… menu steps. 8 RStudio will ask you
whether to save an image of your workspace to your working directory. Since you have not
done any work yet and thus there is nothing to be saved, just click on the Don’t Save button.
8
Alternatively, you can type q() in the Console.
12
ECON20003 - Tutorial 1
Basic Data Handling
Let’s start with a ‘first-year’ exercise to refresh your memory about the various types of data
and measurement scales. 9
Exercise 1
For each of the following examples of data, identify the data type and the measurement scale.
a) The number of daily global deaths due to the COVID-19 pandemic during 2021.
This data arises from simple counting and the basic arithmetic operations make sense on
them, so the data type is quantitative. Moreover, since the possible values are non-negative
integers that can be listed and counted, the data type is discrete quantitative. Finally, the
unit of measurement is ‘one death’ and there is a genuine zero point (no COVID related
death), so the measurement scale is ratio.
In this case the possible values, like economics, marketing, commerce, statistics etc., are
categories, so the data type is qualitative. These departments do not have a natural order,
so the measurement scale is nominal.
One can perform the basic arithmetic operations on price data, so the data type is
quantitative. Moreover, since the possible values can be listed (prices are recorded to the
nearest cent), the data type is discrete quantitative. Finally, the unit of measurement is ‘one
cent’ and there is a genuine zero point (free of charge), so the measurement scale is ratio.
d) The size of soft drink (large, medium or small) ordered by each of a sample of customers
in a restaurant.
The possible values, large, medium and small, are categories, so the data type is
qualitative. They have a natural order, large > medium > small, but we do not know the
exact differences between them, so the measurement scale is ordinal.
e) The number of Toyota cars sold each month in Auckland during 2021.
This example is similar to the first, so the data type is discrete quantitative, and the
measurement scale is ratio.
9
If you do not remember this topic, have a look at slides 5-8 of Review 1 and/or read section 2.1 in the
Selvanathan book.
13
ECON20003 - Tutorial 1
The next exercise illustrates how to enter data to RStudio.
Exercise 2
Consider the table below. It displays the name, gender, age (year), height (cm) and weight (kg)
of six teenagers. Each row is a case, and each column is a variable. Age, Height and Weight
are quantitative variables, while Name and Gender are qualitative variables as they are not
made up of meaningful numbers but letters. 10 Enter this data into RStudio.
Launch RStudio. It is highly recommended to create a new RStudio project every time you start
working on a new tutorial exercise.
An RStudio project is a working directory designated with a RProj file that stores the workspace,
command history and source documents in one place together. Projects are not mandatory for
working in RStudio but they are useful as they make it straightforward to divide your work into
multiple contexts and to separate them from each other.
To create a new project, click File / New Project…. The program will ask you whether to start
the new project in a new working directory or associate it with an existing working directory (see
the screenshot on the next page). 11
10
Note that even if we used some numbers to denote the possible categories, e.g., 1 for female and 2 for male,
Name and Gender still would be qualitative variables because we could not use the associated numbers in any
meaningful calculation. Read the Review 1 slides for further information.
11
There is a third option as well, “Checkout a project from a version control repository”, but it is not relevant for us.
14
ECON20003 - Tutorial 1
By default, an RStudio project inherits the name of the folder where it is saved in. Hence, to
keep every project separate, it is necessary to start every new project in a new working
directory.
For this reason, select the first option, New Directory, and then Project Type: New Project. In
the opening dialogue window (see the first screenshot on the next page) enter t1e2 in the
Directory name box, your preferred root directory, i.e., “file/path”, in the Create project as
subdirectory of box, and click on the Create Project button.
In return, RStudio creates a new folder named t1e2 in your working directory and saves the
[Link] project file in it (see the second screenshot on the next page).
Next, create a new script by following the File / New File / R Script menu steps and then click
on the save icon or follow the File / Save As… menu steps to save the Untitled script under a
new name, say t1e2.
You should always save every new untitled script under a unique name before even starting to
type in it to make sure that you do not lose it unexpectedly if something crashes on your
computer while you are using R.
15
ECON20003 - Tutorial 1
Now you should have two items on the Files tab with the same name, t1e2, but different
extensions, Rproj and R. They are both saved in the t1e2 folder of your working directory.
16
ECON20003 - Tutorial 1
Having initialized the project, we are now ready to enter the data into RStudio. Yet, before doing
so, it is important to introduce a few concepts and definitions.
In every software and programming language the various pieces of information or data used in
a program need to be stored in some reserved memory locations. R does not provide direct
access to these locations but offers several specialized data structures, called objects. R is an
object-based program, everything is treated as an object and is referred to through some
symbol or variable. The symbols themselves are also objects and can be manipulated in the
same way as any other object.
The five most frequently used R-objects or data structures are atomic vector, list, matrix, data
frame, and array. They can be classified by their dimensionality (1 dimensional: atomic vector
and list), 2 dimensional (matrix and data frame), or n dimensional (array), and whether they
contain a single type of contents (homogeneous: atomic vector, matrix and array) or different
types of contents (heterogeneous: list and data frame). Hence, atomic vectors and lists are both
one dimensional, but the former contain a single type of contents while the latter contain
different types of contents. Similarly, matrices and data frames are both 2 dimensional, but the
former contain a single type of contents while the latter contain different types of contents.
R distinguishes six basic data types: character (e.g. “Laszlo”, “True”, “3.14”), numeric 12 (e.g.
201, 3.14), integer (e.g. 2L where L is the integer function that forces 2 to be stored as an
integer, i.e. without decimals), logical (TRUE, FALSE), complex (e.g. 2+3i, where i is an
imaginary number defined as the square root of -1), and raw (used to store the data 'byte by
byte').
In our example, there are five variables (Name, Gender, Age, Height, Weight) and six data
points for each. The first two variables, Name and Gender, are qualitative (also known as
categorical) and the observations on them can be stored in two character-type atomic vectors.
The other three variables (Age, Height, Weight) are quantitative (also known as numerical) and
the observations on them can be stored in three numeric-type atomic vectors.
A data set can be entered in RStudio either by typing it straight from the keyboard in an RStudio
spreadsheet or by importing the data previously saved in the native R format or in a foreign file
format. Although you will usually import data from Excel spreadsheets, it is useful to start with
entering our small data set from the keyboard to an RStudio spreadsheet. 13
12
It is also known as double.
13
In Exercise 3 of Tutorial 2 you will learn how to import the data from Excel to an RStudio spreadsheet.
17
ECON20003 - Tutorial 1
There are two places for typing commands in RStudio, the Source panel and the Console tab.
The main difference between these two options is that the Source panel is just a built-in text
editor, RStudio interacts with R via the Console. Hence, a command that you type in Source is
not evaluated after hitting the Enter key. You need to instruct RStudio to do so by highlighting
the code in the Source panel that you want to be evaluated and clicking on the Run button on
the top right of the Source panel. In return, RStudio sends the highlighted code to the Console,
where R evaluates and executes it.
A command that you type straight in the Console gets executed automatically after you press
Enter. For this reason, typing commands straight in the Console might seem to be the more
convenient and better option, especially when the code is very short. However, not every code
can be executed interactively, i.e., command-by-command, some of them must be entered in
the Source panel and executed together in batch mode. Moreover, the content of the Console
panel is not editable, so if you make a mistake in typing your code into the Console, you need
to re-type everything all over again. Conversely, you can edit your code in the Source panel
and save it for future use. For these reasons, it is highly recommended to use the Source panel
rather than the Console tab right from the start.
To enter our data from the keyboard to an RStudio spreadsheet, type [Link](1) in the
Source panel and click on the Run button in the menu bar of the Source panel.
In return, RStudio echoes the command in the Console and opens the Data Editor window (see
the screenshot on the next page). 14
The RStudio Data Editor looks like an Excel spreadsheet with rows and columns. The active
cell is highlighted by thickening its borders and you can navigate in the spreadsheet by using
the left/right and up/down arrows on your keyboard. There is no constraint on the number of
14
If you have a Mac, for direct data entry from the keyboard you might need to install an extra piece of software.
If you get the error message
“Error in check for XQuartz() : X11 library is missing: install XQuartz from [Link]”,
or something similar, close R / RStudio and download XQuartz from [Link] After installing, run
the software in the background for data entry to work.
18
ECON20003 - Tutorial 1
rows or columns currently in use, the grid is scrolled automatically when you reach the last
visible column or row in the spreadsheet.
As you can see, at this stage both the first variable and its first value are “1”. To rename the
first variable to Name, click on the first header cell, and in the opening Variable editor dialogue
window enter variable name: Name, specify that it has character type values and click X in the
upper right corner (see on the next page).
When you name an R object, you must keep in mind the following rules:
(i) a name can be a combination of letters, numbers and a few special characters, but
it cannot start with a number;
(ii) a name can contain neither the ^, !, $, @, +, -, ?, * special characters nor spaces;
(iii) R is a case sensitive language, so variable ‘A’ and variable ‘a’ are treated as two
different variables in R;
19
ECON20003 - Tutorial 1
(iv) If you name a new object with the name of an existing object, R overwrites any
previous information stored in the existing object without warning or asking for
permission.
Enter the data for Name by typing in the names of the teenagers one by one and hitting return
after each. If you need to navigate, just use the up and down arrows. If an entry does not fit in
the cell, right-click on the column and select the Autosize column option from the opening drop-
down menu.
To create the second variable, click on the second header cell. In the opening Variable editor
dialogue window enter variable name: Gender, select again type: character, and type in the
genders of the teenagers. Create the remaining three variables, Age, Height and Weight,
similarly, except that choose type: numeric for each. This is important because otherwise the
program would treat the values of these variables as strings of characters rather than numbers
and it would be impossible to perform any arithmetic operation on them.
As you can see, the Environment tab displays not only the names of the variables, but also the
type (num or chr), the length ([1:6]) and the elements of the atomic vectors.
At this stage there are five variables (atomic vectors) in your R working environment. They can
be combined into a table, called data frame, which is a two-dimensional array where each
column contains values of one variable and each row contains one set of values from each
column, with the [Link]() function. For example, by typing
21
ECON20003 - Tutorial 1
in the Source panel and clicking on the Run button in the menu bar of the Source panel, you
create the t1e2 data frame, which includes the Age, Gender, Height, Name, and Weight objects.
To see this data frame, type
print(t1e2)
in the Source panel and then click on the Run button. In return, RStudio echoes the command
and displays the content of t1e2 in the Console,
What if we realise that we made a typo while entering the data from the keyboard? Suppose
for example that I misspelled the name of the first teenager and typed ‘Alfrd’ instead of ‘Alfred’,
so this is what I have in the Console:
22
ECON20003 - Tutorial 1
I can correct this mistake by typing
t1e2 = edit(t1e2)
in the Source panel and then clicking on the Run button. In return, RStudio opens the Data
Editor
Now I have to double click Alfrd, change it to Alfred, and close the editor. By executing
print(t1e2)
Any time you enter some data to RStudio, it is recommended to save it. RStudio has four save
toolbar buttons. Two of them are on the left in the main menu bar, the third is below them on
the Source panel, and the fourth is on the right in the Environment/History/Connections panel.
The left save buttons are for saving the actual script you are working on or all open documents,
respectively, while the right save button is for saving your environment (i.e., open files, loaded
variables, loaded libraries).
23
ECON20003 - Tutorial 1
Save this data set as t1e2 in your preferred location, say “file/path”, using the Save button on
the right of your screen in the Environment tab. RStudio echoes this command in the Console
[Link]("file/path/[Link]")
This shows that RStudio saved the file named t1e2 in RData format, which is specific to R and
can store unlimited number of R objects within a single file. It contains not only your data set,
but your entire workspace, i.e., your R working environment including opened files, loaded
variables and libraries. You should now see this new file as well on your Files tab.
Quit RStudio by following the File / Quit Session… menu steps. RStudio warns you that your
RData and R files have unsaved changes. Since you have just created the [Link] file and
have not executed any command since then, there is no need to save the Workspace image.
After RStudio has shut down, open Windows File Explorer and check the t1e2 folder on your
hard drive or USB key, wherever it is. You should have 4 items in the folder: an unnamed
RHistory file, an R file, R Workspace file and an R Project file (see the screenshot on the next
page).
Relaunch RStudio. By default, RStudio returns to the latest project, in this case [Link].
Hence, you should see the content of the t1e2.R file in your Source panel.
24
ECON20003 - Tutorial 1
In addition, on the Files tab in the lower-right panel you should see your three t1e2.* files and
the .Rhistory file that stores the list of commands previously entered in the Console.
25
ECON20003 - Tutorial 1
Exercise for Assessment
Exercise 3
One of the major measures of the quality of service provided by any organisation is the speed
with which the organisation responds to customer complaints. Last year the flooring department
of a large family-owned department store received 50 complaints about carpet installation. The
following data represent the number of days between the receipt and resolution of these
complaints.
Days
54 35 29 2 1
11 126 4 35 26
12 165 27 26 74
13 5 29 22 26
33 137 28 123 14
5 110 52 94 20
19 32 152 25 27
4 27 61 36 5
10 31 29 81 13
68 110 30 31 23
b) Launch RStudio and close the Script tab, if it is open. Create a new RStudio project and
script, and name both t1e3.
c) Enter the observations from your keyboard to an RStudio spreadsheet and save them in
an RData file. Quit RStudio. When prompted, save only the t1e3.R file.
d) Take a screenshot of your Files tab with the Windows Snipping Tool or Snip & Sketch, save
the image as a jpg file and then insert the saved image in the Quiz box.
26
ECON20003 - Tutorial 1