0% found this document useful (0 votes)
12 views159 pages

Lecture 1 Introduction

Uploaded by

ehdidb2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views159 pages

Lecture 1 Introduction

Uploaded by

ehdidb2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 159

Lecture 1:

Introduction

Professor
Amany E. Aly

Getting
Lecture 1: Introduction
started
What is
statistical
programming?
The R package
Why use a
command line?
Professor Amany E. Aly
Installation of R
and RStudio
Getting started
in RStudio
Department of Mathematics, Faculty of Science,
Helwan University, Ain Helwan, Cairo, Egypt.
Introduction
to the R
language
First steps
R can be used
30 / 9 / 2024
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 1 / 99
Presentation Outline

Lecture 1:
Introduction
1 Getting started
Professor What is statistical programming?
Amany E. Aly
The R package
Getting
started
What is
Why use a command line?
statistical
programming?
The R package
Installation of R and RStudio
Why use a
command line?
Installation of R
Getting started in RStudio
and RStudio
Getting started
in RStudio
2 Introduction to the R language
Introduction
to the R
First steps
language R can be used as a calculator
First steps
R can be used
Named storage
as a calculator
Named storage
Quitting R
Quitting R
Basic features of
R
Basic features of R
Functions Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 2 / 99
What is statistical programming?

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
1 Computer programming involves controlling computers, telling
statistical
programming?
The R package
them what calculations to do, what to display, etc.
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 3 / 99
What is statistical programming?

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
1 Computer programming involves controlling computers, telling
statistical
programming?
The R package
them what calculations to do, what to display, etc.
Why use a
command line?
Installation of R
and RStudio
2 Statistical programming is the kind of computer programming
Getting started
in RStudio statisticians do but statisticians do all sorts of programming.
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 3 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
Statisticians are concerned with collecting and analyzing data,
Getting
started
and some statisticians would be involved in setting up connec-
What is
statistical
programming?
tions between computers and laboratory instruments: but we
The R package
Why use a
command line?
would not call that statistical programming.
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 4 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
Statisticians are concerned with collecting and analyzing data,
Getting
started
and some statisticians would be involved in setting up connec-
What is
statistical
programming?
tions between computers and laboratory instruments: but we
The R package
Why use a
command line?
would not call that statistical programming.
Installation of R
and RStudio
Getting started
in RStudio Statisticians often oversee data entry from questionnaires, and
Introduction
to the R may set up programs to aid in detecting data entry errors.
language
First steps
R can be used
That is statistical programming, but it is quite specialized.
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 4 / 99
Lecture 1:
Introduction Statistical programming involves doing computations to aid in s-
Professor
Amany E. Aly tatistical analysis.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 5 / 99
Lecture 1:
Introduction Statistical programming involves doing computations to aid in s-
Professor
Amany E. Aly tatistical analysis.
Getting
started Data must be summarized and displayed,
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 5 / 99
Lecture 1:
Introduction Statistical programming involves doing computations to aid in s-
Professor
Amany E. Aly tatistical analysis.
Getting
started Data must be summarized and displayed,
What is
statistical
programming?
The R package
Models must be fit to data,
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 5 / 99
Lecture 1:
Introduction Statistical programming involves doing computations to aid in s-
Professor
Amany E. Aly tatistical analysis.
Getting
started Data must be summarized and displayed,
What is
statistical
programming?
The R package
Models must be fit to data,
Why use a
command line?
Installation of R
and RStudio
The results must displayed.
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 5 / 99
Lecture 1:
Introduction Statistical programming involves doing computations to aid in s-
Professor
Amany E. Aly tatistical analysis.
Getting
started Data must be summarized and displayed,
What is
statistical
programming?
The R package
Models must be fit to data,
Why use a
command line?
Installation of R
and RStudio
The results must displayed.
Getting started
in RStudio
Drawing graphics of one, two or higher dimensional data is an
Introduction
to the R
language
aspect of statistical programming.
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 5 / 99
Lecture 1:
Introduction Statistical programming involves doing computations to aid in s-
Professor
Amany E. Aly tatistical analysis.
Getting
started Data must be summarized and displayed,
What is
statistical
programming?
The R package
Models must be fit to data,
Why use a
command line?
Installation of R
and RStudio
The results must displayed.
Getting started
in RStudio
Drawing graphics of one, two or higher dimensional data is an
Introduction
to the R
language
aspect of statistical programming.
First steps
R can be used
as a calculator
Named storage
These tasks can be done in a number of different computer appli-
Quitting R
Basic features of
R
cations: Microsoft Excel, SAS, SPSS, R, Stata, etc.
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 5 / 99
Lecture 1:
Introduction

Professor An important part of statistical programming is stochastic sim-


Amany E. Aly
ulation.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 6 / 99
Lecture 1:
Introduction

Professor An important part of statistical programming is stochastic sim-


Amany E. Aly
ulation.
Getting
started
What is
Digital computers are naturally very good at exact, repro-
statistical
programming?
The R package
ducible computations, but the real world is full of randomness.
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 6 / 99
Lecture 1:
Introduction

Professor An important part of statistical programming is stochastic sim-


Amany E. Aly
ulation.
Getting
started
What is
Digital computers are naturally very good at exact, repro-
statistical
programming?
The R package
ducible computations, but the real world is full of randomness.
Why use a
command line?
Installation of R
and RStudio
In stochastic simulation we program a computer to act as
Getting started
in RStudio though it is producing random results, even though if we knew
Introduction
to the R
language
enough, the results would be exactly predictable.
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 6 / 99
Lecture 1:
Introduction

Professor An important part of statistical programming is stochastic sim-


Amany E. Aly
ulation.
Getting
started
What is
Digital computers are naturally very good at exact, repro-
statistical
programming?
The R package
ducible computations, but the real world is full of randomness.
Why use a
command line?
Installation of R
and RStudio
In stochastic simulation we program a computer to act as
Getting started
in RStudio though it is producing random results, even though if we knew
Introduction
to the R
language
enough, the results would be exactly predictable.
First steps
R can be used
as a calculator
Statistical programming involves optimization, and approxi-
Named storage
Quitting R
Basic features of
mation of mathematical functions.
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 6 / 99
The R package

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
R, is an open source package for statistical computing.
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 7 / 99
The R package

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
R, is an open source package for statistical computing.
statistical
programming?
The R package
Why use a
”Open source” has a number of different meanings; the im-
command line?
Installation of R
and RStudio
portant one is that R is freely available, and its users are free
Getting started
in RStudio to see how it is written, and to improve it.
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 7 / 99
Why use a command line?

Lecture 1:
Introduction

Professor The R system is mainly command-driven, with the user typing


Amany E. Aly
in text and asking R to execute it.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 8 / 99
Why use a command line?

Lecture 1:
Introduction

Professor The R system is mainly command-driven, with the user typing


Amany E. Aly
in text and asking R to execute it.
Getting
started
What is If you want to program a computer to do something that no
statistical
programming?
The R package one has done before, you can easily do it by breaking down
Why use a
command line?
Installation of R
and RStudio
the task into the parts that make it up, and then building up
Getting started
in RStudio a program to carry it out.
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 8 / 99
Why use a command line?

Lecture 1:
Introduction

Professor The R system is mainly command-driven, with the user typing


Amany E. Aly
in text and asking R to execute it.
Getting
started
What is If you want to program a computer to do something that no
statistical
programming?
The R package one has done before, you can easily do it by breaking down
Why use a
command line?
Installation of R
and RStudio
the task into the parts that make it up, and then building up
Getting started
in RStudio a program to carry it out.
Introduction
to the R
language
Learning how to use one command-line interface will give you
First steps
R can be used
as a calculator
skills that carry over to others, and may even give you some
Named storage
Quitting R insight into how a menu-driven interface is implemented.
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 8 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly As statisticians, it is our belief that your goal should be un-
Getting
derstanding, and learning how to program at a command line
started
What is
statistical
will give you that at a fundamental level.
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 9 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly As statisticians, it is our belief that your goal should be un-
Getting
derstanding, and learning how to program at a command line
started
What is
statistical
will give you that at a fundamental level.
programming?
The R package
Why use a
Learning to use a menu-based program makes you dependent
command line?
Installation of R
and RStudio on the particular organization of that program.
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 9 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly As statisticians, it is our belief that your goal should be un-
Getting
derstanding, and learning how to program at a command line
started
What is
statistical
will give you that at a fundamental level.
programming?
The R package
Why use a
Learning to use a menu-based program makes you dependent
command line?
Installation of R
and RStudio on the particular organization of that program.
Getting started
in RStudio

Introduction
There is no question that command-line interfaces require
to the R
language greater knowledge on the part of the user-you need to re-
First steps
R can be used
as a calculator member what to type to achieve a particular outcome.
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 9 / 99
Installation of R and RStudio

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
R can be downloaded from https://cloud.r-project.org.
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 10 / 99
Installation of R and RStudio

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
R can be downloaded from https://cloud.r-project.org.
What is
statistical
programming? R is designed to be very portable: it will run on Microsoft
The R package
Why use a
command line? Windows, Linux, Solaris, macOS, and other operating systems,
Installation of R
and RStudio
Getting started
in RStudio
but different binary versions are required for each.
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 10 / 99
Installation of R and RStudio

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
R can be downloaded from https://cloud.r-project.org.
What is
statistical
programming? R is designed to be very portable: it will run on Microsoft
The R package
Why use a
command line? Windows, Linux, Solaris, macOS, and other operating systems,
Installation of R
and RStudio
Getting started
in RStudio
but different binary versions are required for each.
Introduction
to the R
Installation on Microsoft Windows is straightforward.
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 10 / 99
Lecture 1:
Introduction

Professor
A binary version is available for Windows Vista or above from
Amany E. Aly
the web page https://cloud.r-project.org/bin/windows/base.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 11 / 99
Lecture 1:
Introduction

Professor
A binary version is available for Windows Vista or above from
Amany E. Aly
the web page https://cloud.r-project.org/bin/windows/base.
Getting
started
What is
Download the ”setup program,” a file with a name like R-
statistical
programming?
The R package
4.0.2-win.exe.
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 11 / 99
Lecture 1:
Introduction

Professor
A binary version is available for Windows Vista or above from
Amany E. Aly
the web page https://cloud.r-project.org/bin/windows/base.
Getting
started
What is
Download the ”setup program,” a file with a name like R-
statistical
programming?
The R package
4.0.2-win.exe.
Why use a
command line?
Installation of R
and RStudio
Clicking on this file will start an almost automatic installation
Getting started
in RStudio of the R system.
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 11 / 99
Lecture 1:
Introduction

Professor
A binary version is available for Windows Vista or above from
Amany E. Aly
the web page https://cloud.r-project.org/bin/windows/base.
Getting
started
What is
Download the ”setup program,” a file with a name like R-
statistical
programming?
The R package
4.0.2-win.exe.
Why use a
command line?
Installation of R
and RStudio
Clicking on this file will start an almost automatic installation
Getting started
in RStudio of the R system.
Introduction
to the R
language
Though it is possible to customize the installation, the de-
First steps
R can be used
as a calculator
fault responses will lead to a satisfactory installation in most
Named storage
Quitting R situations, particularly for beginning users.
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 11 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
One of the default settings of the installation procedure is to
Getting
create an R icon on your computer’s desktop.
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 12 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
One of the default settings of the installation procedure is to
Getting
create an R icon on your computer’s desktop.
started
What is
statistical
You should also install RStudio, after you have installed R.
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 12 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
One of the default settings of the installation procedure is to
Getting
create an R icon on your computer’s desktop.
started
What is
statistical
You should also install RStudio, after you have installed R.
programming?
The R package
Why use a
command line?
As with R, there are separate versions for different computing
Installation of R
and RStudio
Getting started
platforms, but they all look and act similarly.
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 12 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
One of the default settings of the installation procedure is to
Getting
create an R icon on your computer’s desktop.
started
What is
statistical
You should also install RStudio, after you have installed R.
programming?
The R package
Why use a
command line?
As with R, there are separate versions for different computing
Installation of R
and RStudio
Getting started
platforms, but they all look and act similarly.
in RStudio

Introduction You should download the ”Open Source Edition” of ”RStudio


to the R
language
First steps
Desktop” from www.rstudio.com, and follow the instructions
R can be used
as a calculator
Named storage
to install it on your computer.
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 12 / 99
Getting started in RStudio

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
When you are working in RStudio, you’ll see a display something
Why use a
command line?
Installation of R
like Figure 1.1.
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 13 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 14 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
The display includes four panes.
What is
statistical
programming?
The top left pane is the Source Pane, or editor. You will type
The R package
Why use a
command line?
your program (or other document) there. You can have several
Installation of R
and RStudio
Getting started
open files in this pane; the tabs allow you to choose which is
in RStudio

Introduction active.
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 15 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
The bottom left pane is called the Console Pane. This is where
Getting
started you communicate with R. You can type directly into this pane,
What is
statistical
programming? but it is usually better to work within the editor pane, because
The R package
Why use a
command line? that way you can easily correct mistakes and try again.
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 16 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
The bottom left pane is called the Console Pane. This is where
Getting
started you communicate with R. You can type directly into this pane,
What is
statistical
programming? but it is usually better to work within the editor pane, because
The R package
Why use a
command line? that way you can easily correct mistakes and try again.
Installation of R
and RStudio
Getting started
in RStudio
The two right-hand panes contain a variety of tabs. In the
Introduction
to the R
figure, the top pane is showing the Environment Pane (i.e.
language
First steps the workspace), and the bottom pane is showing a plot.
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 16 / 99
Lecture 1:
Introduction For now, you just need to know the following points:
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 17 / 99
Lecture 1:
Introduction For now, you just need to know the following points:
Professor
Amany E. Aly You should do most of your work in the editor, but you can
Getting occasionally type in the console.
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 17 / 99
Lecture 1:
Introduction For now, you just need to know the following points:
Professor
Amany E. Aly You should do most of your work in the editor, but you can
Getting occasionally type in the console.
started
What is
statistical
programming?
The console pane displays what R is doing.
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 17 / 99
Lecture 1:
Introduction For now, you just need to know the following points:
Professor
Amany E. Aly You should do most of your work in the editor, but you can
Getting occasionally type in the console.
started
What is
statistical
programming?
The console pane displays what R is doing.
The R package
Why use a
command line?
All of the panes can be resized and repositioned, so sometimes
Installation of R
and RStudio
Getting started
it may appear that you’ve lost one, but there’s no need to
in RStudio

Introduction worry: just find the header of the pane and click there with
to the R
language
First steps
your mouse, and the pane will reappear.
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 17 / 99
Lecture 1:
Introduction For now, you just need to know the following points:
Professor
Amany E. Aly You should do most of your work in the editor, but you can
Getting occasionally type in the console.
started
What is
statistical
programming?
The console pane displays what R is doing.
The R package
Why use a
command line?
All of the panes can be resized and repositioned, so sometimes
Installation of R
and RStudio
Getting started
it may appear that you’ve lost one, but there’s no need to
in RStudio

Introduction worry: just find the header of the pane and click there with
to the R
language
First steps
your mouse, and the pane will reappear.
R can be used
as a calculator
Named storage
If the pane is there but the content isn’t what you want, try
Quitting R
Basic features of
R
clicking on the tabs at the top.
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 17 / 99
Presentation Outline

Lecture 1:
Introduction
1 Getting started
Professor What is statistical programming?
Amany E. Aly
The R package
Getting
started
What is
Why use a command line?
statistical
programming?
The R package
Installation of R and RStudio
Why use a
command line?
Installation of R
Getting started in RStudio
and RStudio
Getting started
in RStudio
2 Introduction to the R language
Introduction
to the R
First steps
language R can be used as a calculator
First steps
R can be used
Named storage
as a calculator
Named storage
Quitting R
Quitting R
Basic features of
R
Basic features of R
Functions Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 18 / 99
First steps

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
Having opened R or RStudio, you may begin entering and
started
What is
statistical
executing commands.
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 19 / 99
First steps

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
Having opened R or RStudio, you may begin entering and
started
What is
statistical
executing commands.
programming?
The R package
Why use a
Use the Source Pane to type in your commands, or use the
command line?
Installation of R
and RStudio Console Pane directly.
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 19 / 99
First steps

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
Having opened R or RStudio, you may begin entering and
started
What is
statistical
executing commands.
programming?
The R package
Why use a
Use the Source Pane to type in your commands, or use the
command line?
Installation of R
and RStudio Console Pane directly.
Getting started
in RStudio

Introduction
The greater-than sign (>) is the prompt symbol which appears
to the R
language in the Console Pane.
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 19 / 99
R can be used as a calculator

Lecture 1:
Introduction
The basic operations are + (add), − (subtract), ∗ (multiply),
Professor
Amany E. Aly
and / (divide).
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 20 / 99
R can be used as a calculator

Lecture 1:
Introduction
The basic operations are + (add), − (subtract), ∗ (multiply),
Professor
Amany E. Aly
and / (divide).
Getting
started
For example, try
What is
statistical
programming?
5504982/131071
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 20 / 99
R can be used as a calculator

Lecture 1:
Introduction
The basic operations are + (add), − (subtract), ∗ (multiply),
Professor
Amany E. Aly
and / (divide).
Getting
started
For example, try
What is
statistical
programming?
5504982/131071
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio
Upon pressing the Enter key (or CTRL-Enter, or CMD-Enter,
Introduction
to the R
depending on your system), the result of the above division op-
language
First steps
R can be used
eration, 42, appears in the Console Pane, preceded by the com-
as a calculator
Named storage
Quitting R
mand you executed, and prefixed by the number 1 in square
Basic features of
R brackets:
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 20 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 21 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio The [1] indicates that this is the first (and in this case only) result
Introduction
to the R from the command. Many commands return multiple values, and
language
First steps
R can be used
each line of results will be labeled to aid the user in deciphering
as a calculator
Named storage
Quitting R
the output.
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 21 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
For example, the sequence of integers from 17 to 58 may be dis-
played as follows:
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
The first line starts with the first return value, so is labeled [1];
R can be used
as a calculator
Named storage
the second line starts with the 23rd, so is labeled [23].
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 22 / 99
Lecture 1:
Introduction

Professor Everything that you type after a # sign is assumed to be a


Amany E. Aly
comment and is ignored by R.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 23 / 99
Lecture 1:
Introduction

Professor Everything that you type after a # sign is assumed to be a


Amany E. Aly
comment and is ignored by R.
Getting
started
What is Parentheses are used to ensure that the operations (like, :, ∗
statistical
programming?
The R package and +) are carried out in the order that we desire.
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 23 / 99
Lecture 1:
Introduction

Professor Everything that you type after a # sign is assumed to be a


Amany E. Aly
comment and is ignored by R.
Getting
started
What is Parentheses are used to ensure that the operations (like, :, ∗
statistical
programming?
The R package and +) are carried out in the order that we desire.
Why use a
command line?
Installation of R
and RStudio
R can also be used to compute powers with the b operator.
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 23 / 99
Lecture 1:
Introduction Modular arithmetic is also available. You can compute the
Professor
Amany E. Aly remainder after division of 31 by 7, i.e. 31 (mod 7):
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 24 / 99
Lecture 1:
Introduction Modular arithmetic is also available. You can compute the
Professor
Amany E. Aly remainder after division of 31 by 7, i.e. 31 (mod 7):
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
While the integer part of a fraction can be computed as
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 24 / 99
Named storage

Lecture 1:
Introduction

Professor
R has a workspace known as the global environment that can
Amany E. Aly
be used to store the results of calculations, and many other
Getting
started types of objects. To do this, type
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 25 / 99
Named storage

Lecture 1:
Introduction

Professor
R has a workspace known as the global environment that can
Amany E. Aly
be used to store the results of calculations, and many other
Getting
started types of objects. To do this, type
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio
You can see the results of this assignment by typing the name
Introduction
to the R
of our new object at the prompt:
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 25 / 99
Example 2.1

Lecture 1:
Introduction

Professor
Amany E. Aly
An individual wishes to take out a loan, today, of P at a monthly
interest rate i. The loan is to be paid back in n monthly install-
Getting
started
What is
ments of size R, beginning one month from now. The problem is
statistical
programming?
The R package
to calculate R.
Why use a
command line?
Installation of R
This is the formula for the present value of an annuity. We can
and RStudio
Getting started
in RStudio find R, given P , n and i as
Introduction
to the R
language
i
R=P
1 − (1 + i)−n
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 26 / 99
Solution of Example 2.1

Lecture 1:
Introduction

Professor
Amany E. Aly
In R, we define variables as follows: principal to hold the value
Getting
started of P, and intRate to hold the interest rate, and n to hold the
What is
statistical
programming?
The R package
number of payments. We will assign the resulting payment
Why use a
command line?
Installation of R
value to an object called payment.
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 27 / 99
Solution of Example 2.1

Lecture 1:
Introduction

Professor
Amany E. Aly
In R, we define variables as follows: principal to hold the value
Getting
started of P, and intRate to hold the interest rate, and n to hold the
What is
statistical
programming?
The R package
number of payments. We will assign the resulting payment
Why use a
command line?
Installation of R
value to an object called payment.
and RStudio
Getting started
in RStudio We need some numerical values to work with, so we will sup-
Introduction
to the R pose that the loan amount is $1500, the interest rate is 1%
language
First steps
R can be used
and the number of payments is 10. The required code is then
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 27 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 28 / 99
Quitting R

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started To quit your R session, run
What is
statistical
programming? q()
The R package
Why use a
command line?
or choose Quit Session... from the File menu.You will then be
Installation of R
and RStudio
Getting started
asked whether to save an image of the current workspace, or not,
in RStudio

Introduction or to cancel.
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 29 / 99
Recording your work

Lecture 1:
Introduction

Professor
Amany E. Aly
Rather than saving the workspace, we prefer to keep a record
Getting
started of the commands we entered, so that we can reproduce the
What is
statistical
programming? workspace at a later date.
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 30 / 99
Recording your work

Lecture 1:
Introduction

Professor
Amany E. Aly
Rather than saving the workspace, we prefer to keep a record
Getting
started of the commands we entered, so that we can reproduce the
What is
statistical
programming? workspace at a later date.
The R package
Why use a
command line?
Installation of R
The easiest way to do this in RStudio is to enter commands
and RStudio
Getting started
in RStudio
in the Source Pane, and run them from there.
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 30 / 99
Recording your work

Lecture 1:
Introduction

Professor
Amany E. Aly
Rather than saving the workspace, we prefer to keep a record
Getting
started of the commands we entered, so that we can reproduce the
What is
statistical
programming? workspace at a later date.
The R package
Why use a
command line?
Installation of R
The easiest way to do this in RStudio is to enter commands
and RStudio
Getting started
in RStudio
in the Source Pane, and run them from there.
Introduction
to the R At the end of a session, save the final script for a permanent
language
First steps
R can be used
record of your work.
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 30 / 99
Exercises

Lecture 1:
Introduction

Professor (1) Calculate the remainder after dividing 31079 into 170166719.
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 31 / 99
Exercises

Lecture 1:
Introduction

Professor (1) Calculate the remainder after dividing 31079 into 170166719.
Amany E. Aly

Getting
(2) Calculate the interest earned after 5 years on an investment of
started
What is
$2000, assuming an interest rate of 3% compounded annually.
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 31 / 99
Exercises

Lecture 1:
Introduction

Professor (1) Calculate the remainder after dividing 31079 into 170166719.
Amany E. Aly

Getting
(2) Calculate the interest earned after 5 years on an investment of
started
What is
$2000, assuming an interest rate of 3% compounded annually.
statistical
programming?
The R package
Why use a
(3) Using one line of R code, calculate the interest earned on an
command line?
Installation of R
and RStudio
investment of $2000, assuming an interest rate of 3% com-
Getting started
in RStudio pounded annually, for terms of 1, 2,..., 30 years.
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 31 / 99
Exercises

Lecture 1:
Introduction

Professor (1) Calculate the remainder after dividing 31079 into 170166719.
Amany E. Aly

Getting
(2) Calculate the interest earned after 5 years on an investment of
started
What is
$2000, assuming an interest rate of 3% compounded annually.
statistical
programming?
The R package
Why use a
(3) Using one line of R code, calculate the interest earned on an
command line?
Installation of R
and RStudio
investment of $2000, assuming an interest rate of 3% com-
Getting started
in RStudio pounded annually, for terms of 1, 2,..., 30 years.
Introduction
to the R
language (4) Calculate the monthly payment required for a loan of $200,000,
First steps
R can be used
as a calculator
at a monthly interest rate of 0.003, based on 300 monthly pay-
Named storage
Quitting R
Basic features of
ments, starting in one month’s time.
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 31 / 99
Exercises

Lecture 1:
Introduction

Professor (5) Use R to calculate the area of a circle with radius 7 cm.
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 32 / 99
Exercises

Lecture 1:
Introduction

Professor (5) Use R to calculate the area of a circle with radius 7 cm.
Amany E. Aly

Getting
(6) Using one line of R code, calculate the respective areas of the
started
What is
circles having radii 3,4, . . . ,100.
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 32 / 99
Exercises

Lecture 1:
Introduction

Professor (5) Use R to calculate the area of a circle with radius 7 cm.
Amany E. Aly

Getting
(6) Using one line of R code, calculate the respective areas of the
started
What is
circles having radii 3,4, . . . ,100.
statistical
programming?
The R package
Why use a
(7) In the expression 48:(14*3), are the brackets really necessary?
command line?
Installation of R
and RStudio
What happens when you type 48:14*3?
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 32 / 99
Exercises

Lecture 1:
Introduction

Professor (5) Use R to calculate the area of a circle with radius 7 cm.
Amany E. Aly

Getting
(6) Using one line of R code, calculate the respective areas of the
started
What is
circles having radii 3,4, . . . ,100.
statistical
programming?
The R package
Why use a
(7) In the expression 48:(14*3), are the brackets really necessary?
command line?
Installation of R
and RStudio
What happens when you type 48:14*3?
Getting started
in RStudio

Introduction
(8) Do you think there is a difference between
to the R
language 48 : 142 and 48 : (142 )? Try both calculations. Using one line
First steps
R can be used
as a calculator
of code, how would you obtain the squares of the numbers
Named storage
Quitting R
Basic features of
48,47, . . . ,14?
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 32 / 99
Functions

Lecture 1:
Introduction Most of the work in R is done through functions.
Professor
Amany E. Aly To quit R we can type q().
Getting This tells R to call the function named q. The parentheses sur-
started
What is
statistical
round the argument list, which in this case contains nothing:
programming?
The R package
Why use a
we just want R to quit, and do not need to tell it how.
command line?
Installation of R
and RStudio q is defined as
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 33 / 99
Lecture 1:
Introduction

Professor This shows that q is a function that has three arguments:


Amany E. Aly
save , status, and runLast . Each of those has a default value:
Getting
started ”default”, 0, and TRUE, respectively. What happens when we
What is
statistical
programming?
The R package
execute q() is that R calls the q function with the arguments
Why use a
command line?
Installation of R
set to their default values.
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 34 / 99
Lecture 1:
Introduction

Professor This shows that q is a function that has three arguments:


Amany E. Aly
save , status, and runLast . Each of those has a default value:
Getting
started ”default”, 0, and TRUE, respectively. What happens when we
What is
statistical
programming?
The R package
execute q() is that R calls the q function with the arguments
Why use a
command line?
Installation of R
set to their default values.
and RStudio
Getting started
in RStudio If we want to change the default values, we specify them when
Introduction
to the R we call the function.
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 34 / 99
Lecture 1:
Introduction

Professor This shows that q is a function that has three arguments:


Amany E. Aly
save , status, and runLast . Each of those has a default value:
Getting
started ”default”, 0, and TRUE, respectively. What happens when we
What is
statistical
programming?
The R package
execute q() is that R calls the q function with the arguments
Why use a
command line?
Installation of R
set to their default values.
and RStudio
Getting started
in RStudio If we want to change the default values, we specify them when
Introduction
to the R we call the function.
language
First steps
R can be used
Arguments are identified in the call by their position, or by
as a calculator
Named storage
Quitting R
specifying the name explicitly.
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 34 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio tell R to call q with the first argument set to ”no”, i.e. to quit
Introduction
to the R
language
without saving the workspace. If we had given two arguments
First steps
R can be used
without names, they would apply to save and status.
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 35 / 99
Lecture 1:
Introduction If we want to accept the defaults of the early parameters but
Professor
Amany E. Aly change later ones, we give the name when calling the function,
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio or use commas to mark the missing arguments,
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 36 / 99
Notes

Lecture 1:
Introduction

Professor
Amany E. Aly
We must use = to set arguments.
Getting
started
If we had written q(runLast < − FALSE) it would be inter-
What is
statistical
programming?
preted quite differently from q(runLast = FALSE). The arrow
The R package
Why use a
command line?
says to put the value FALSE into a variable named runLast.
Installation of R
and RStudio
Getting started
We then pass the result of that action (which is the value
in RStudio

Introduction
FALSE) as the first argument of q(). Since save is the first
to the R
language argument, it will act like q(save = FALSE), which is probably
First steps
R can be used
as a calculator not what we wanted.
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 37 / 99
Notes

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
It is a good idea to use named arguments when calling a func-
statistical
programming?
The R package tion which has many arguments or when using uncommon
Why use a
command line?
Installation of R
and RStudio
arguments, because it reduces the risk of specifying the wrong
Getting started
in RStudio argument, and makes your code easier to read.
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 38 / 99
R is case-sensitive

Lecture 1:
Introduction Consider
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 39 / 99
R is case-sensitive

Lecture 1:
Introduction Consider
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line? Now try
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
The mean() is built in function. R considers MEAN to be a dif-
Basic features of
R
Functions
ferent one, because it is case-sensitive: m is different from M.
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 39 / 99
Listing the objects in the workspace

Lecture 1:
Introduction

Professor The calculations in the previous sections led to the creation of


Amany E. Aly
several simple R objects. These objects are stored in the current
Getting
started R workspace. A list of all objects in the current workspace can be
What is
statistical
programming?
The R package
printed to the screen using the objects() function:
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 40 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
A synonym for objects() is ls().
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 41 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
A synonym for objects() is ls().
programming?
The R package
Why use a
command line?
In RStudio the Environment Pane shows both the names and
Installation of R
and RStudio
Getting started
abbreviated displays of the objects’ values.
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 41 / 99
Numeric vectors

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
A numeric vector is a list of numbers.
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 42 / 99
Numeric vectors

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
A numeric vector is a list of numbers.
The R package
Why use a
command line?
The c() function is used to collect things together into a vec-
Installation of R
and RStudio
Getting started
tor.
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 42 / 99
Lecture 1:
Introduction We can type
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 43 / 99
Lecture 1:
Introduction We can type
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line? Or
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
To see the contents of x
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 43 / 99
Lecture 1:
Introduction The : symbol can be used to create sequences of increasing
Professor
Amany E. Aly
(or decreasing) values.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 44 / 99
Lecture 1:
Introduction The : symbol can be used to create sequences of increasing
Professor
Amany E. Aly
(or decreasing) values.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio
Vectors can be joined together (i.e. concatenated) with the
Introduction
to the R
c() function.
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 44 / 99
Lecture 1:
Introduction Another use of the c() function.
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
If you type this in the R console (not in the RStudio Source Pane),
in RStudio

Introduction
R will prompt you with a + sign for the second line of input.
to the R
language RStudio doesn’t add the prompt, but it will indent the second line.
First steps
R can be used
as a calculator In both cases you are being told that the first line is incomplete:
Named storage
Quitting R
Basic features of
you have an open parenthesis.
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 45 / 99
Lecture 1:
Introduction

Professor Append numbers5to20 to the end of some.numbers, and then


Amany E. Aly
append the decreasing sequence from 4 to 1:
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps The numbers printed in square brackets give the index of the
R can be used
as a calculator
Named storage element immediately to the right.
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 46 / 99
Extracting elements from vectors

Lecture 1:
Introduction To display the 22nd element of a.mess is to use square brackets
Professor
Amany E. Aly to extract just that element:
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 47 / 99
Extracting elements from vectors

Lecture 1:
Introduction To display the 22nd element of a.mess is to use square brackets
Professor
Amany E. Aly to extract just that element:
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio
We can extract more than one element at a time. For example,
Introduction the third, sixth, and seventh elements of a.mess are
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 47 / 99
Lecture 1:
Introduction To get the third through seventh element of numbers5to20,
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 48 / 99
Lecture 1:
Introduction To get the third through seventh element of numbers5to20,
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
Negative indices can be used to avoid certain elements. For ex-
and RStudio
Getting started
in RStudio
ample, we can select all but the second and tenth elements of
Introduction
to the R
numbers5to20
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 48 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting The third through eleventh elements of numbers5to20 can be


started
What is
statistical
avoided as follows:
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 49 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly Do not mix positive and negative indices. To see what happens,
Getting observe
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
The problem is that it is not clear what is to be extracted: do we
to the R
language want the third element of x before or after removing the second
First steps
R can be used
as a calculator
one?
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 50 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly Always be careful to make sure that vector indices are integers.
Getting When fractional values are used, they will be truncated towards
started
What is
statistical
0. Thus 0.6 becomes 0, as in
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
The output numeric(0) indicates a numeric vector of length zero.
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 51 / 99
Vector arithmetic

Lecture 1:
Introduction

Professor
Amany E. Aly
Arithmetic can be done on R vectors. We can multiply all elements
Getting
started
What is
of x by 3:
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
The computation is performed elementwise.
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 52 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
Addition, subtraction, and division by a constant have the same
Getting
started kind of effect.
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 53 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
Taking the 3rd power of the elements of x:
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 54 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly In general, the binary operators also work element-by-element
Getting
started
when applied to pairs of vectors. For example, we can compute
What is
statistical
programming?
yixi , for i = 1, 2, and 3 as follows:
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 55 / 99
Lecture 1:
Introduction

Professor When the vectors are different lengths, the shorter one is extended
Amany E. Aly
by recycling: values are repeated, starting at the beginning.
Getting
started
What is
statistical
programming?
The R package For example, to see the pattern of remainders of the numbers 1
Why use a
command line?
Installation of R
and RStudio
to 10 modulo 2 and 3, we need only give the 2:3 vector once:
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 56 / 99
Lecture 1:
Introduction R will give a warning if the length of the longer vector is not a
Professor
Amany E. Aly multiple of the length of the smaller one, because that is often a
Getting
symptom of an error in the code.
started
What is
statistical
programming?
The R package
Why use a
command line?
For example, if we wanted the remainders modulo 2, 3, and 4,
Installation of R
and RStudio
Getting started
this is the wrong way to do it:
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 57 / 99
Simple patterned vectors

Lecture 1:
Introduction

Professor
Patterned vectors can also be produced using the seq() function
Amany E. Aly
as well as the rep() function.
Getting
started
What is
statistical
programming?
The R package
For example, the sequence of odd numbers less than or equal to
Why use a
command line?
Installation of R
21 can be obtained using
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Notice the use of by = 2 here.
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 58 / 99
Simple patterned vectors

Lecture 1:
Introduction The seq() function has several optional parameters, including one
Professor
Amany E. Aly named by. If by is not specified, the default value of 1 will be
Getting used. Repeated patterns are obtained using rep().
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 59 / 99
Vectors with random patterns

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
The sample() function allows us to simulate things like the results
What is
statistical
programming?
of the repeated tossing of a 6-sided die.
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 60 / 99
Character vectors

Lecture 1:
Introduction Scalars and vectors can be made up of strings of characters instead
Professor
Amany E. Aly of numbers. All elements of a vector must be of the same type.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 61 / 99
Character vectors

Lecture 1:
Introduction Scalars and vectors can be made up of strings of characters instead
Professor
Amany E. Aly of numbers. All elements of a vector must be of the same type.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio To see the contents of more.colors and z, simply type
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 61 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
For substrings, use substr(x, start, stop), where x is a vector of
Getting
started character strings, and start and stop say which characters to keep.
What is
statistical
programming? For example, to print the first two letters of each color use
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 62 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
The other basic operation is building up strings by concatenation
Getting
started
What is
within elements. Use the paste() function for this.
statistical
programming?
The R package
For example,
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 63 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
There are two optional parameters to paste().
Getting
(1) The sep parameter controls what goes between the compo-
started
What is
statistical
nents being pasted together. We might not want the default
programming?
The R package space, for example:
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 64 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting The paste0() function is a shorthand way to set sep = ””:


started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 65 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly (2) The collapse parameter to paste() allows all the components
Getting
started
of the resulting vector to be collapsed into a single string:
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 66 / 99
Factors

Lecture 1:
Introduction

Professor Factors offer an alternative way to store character data.


Amany E. Aly
For example, a factor with four elements and having the two levels,
Getting
started
What is
control and treatment can be created using:
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 67 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly Factors can be an efficient way of storing character data when
Getting there are repeats among the vector elements. This is because the
started
What is
statistical
levels of a factor are internally coded as integers. To see what the
programming?
The R package
Why use a
codes are for our factor, we can type
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 68 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
The labels for the levels are only stored once each, rather than
Getting
being repeated. The codes are indices of the vector of levels:
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 69 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
The levels() function can be used to change factor labels as well.
Getting
started
What is
For example, suppose we wish to change the ”control” label to
statistical
programming?
The R package
”placebo”. Since ”control” is the first level, we change the first
Why use a
command line?
Installation of R
element of the levels(grp) vector:
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 70 / 99
Lecture 1:
Introduction

Professor
An important use for factors is to list all possible values, even if
Amany E. Aly
some are not present. For example,
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
shows that there are two possible values for sex, but only one is
as a calculator
Named storage
Quitting R
present in our vector.
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 71 / 99
More on extracting elements from vectors

Lecture 1:
Introduction As for numeric vectors, square brackets [ ] are used to index factor
Professor
Amany E. Aly and character vector elements. To print the third element of the
Getting factor grp which has four elements,
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 72 / 99
More on extracting elements from vectors

Lecture 1:
Introduction As for numeric vectors, square brackets [ ] are used to index factor
Professor
Amany E. Aly and character vector elements. To print the third element of the
Getting factor grp which has four elements,
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language To access the second through fifth elements of more.colors
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 72 / 99
Matrices and arrays

Lecture 1:
Introduction To arrange values into a matrix, we use the matrix() function:
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 73 / 99
Matrices and arrays

Lecture 1:
Introduction To arrange values into a matrix, we use the matrix() function:
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
To get the value in the first row, and second column
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 73 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started R allows a matrix to be indexed as a vector, using just one value:
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 74 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
Elements are selected in the order in which they are stored in-
Getting
started ternally: down the first column, then down the second, and so
What is
statistical
programming? on.
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 75 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
Elements are selected in the order in which they are stored in-
Getting
started ternally: down the first column, then down the second, and so
What is
statistical
programming? on.
The R package
Why use a
command line? This is known as column-major storage order. Some computer
Installation of R
and RStudio
Getting started
in RStudio
languages use row-major storage order, where values are stored
Introduction in order from left to right across the first row, then left to right
to the R
language
First steps
across the second, and so on.
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 75 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly Whole rows or columns of matrices may be selected by leaving
Getting
started
one index blank:
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 76 / 99
Lecture 1:
Introduction A more general way to store data is in an array. Arrays have
Professor
Amany E. Aly
multiple indices, and are created using the array function:
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 77 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
Notice that the dimensions were specified in a vector c(3, 4, 2).
programming?
The R package
Why use a
When inserting data, the first index varies fastest; when it has run
command line?
Installation of R
and RStudio
through its full range, the second index changes, etc.
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 78 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package Solve Exercises from 1 to 12 on page 21.
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 79 / 99
Approximate storage of numbers

Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
Since R only stores 53 bits, it won’t be able to store decimal
started
What is
statistical
numbers exactly. Some rounding error will occur in the storage.
programming?
The R package
Why use a
We can observe the rounding error with the following experiment.
command line?
Installation of R
and RStudio
With exact arithmetic, (5/4)∗(4/5) = 1, so (5/4)∗(n∗4/5) should
Getting started
in RStudio be exactly n for any value of n. But if we try this calculation in
Introduction
to the R
language
R, we find
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 80 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R i.e. it is equal for some values, but not equal for n = 3, 6, or 7.
language
First steps
R can be used
The errors are very small, but non-zero.
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 81 / 99
Example 2.2

Lecture 1:
Introduction

Professor
Amany E. Aly
Consider the standard formula for the sample variance of a sample
Getting
started
What is
x1 , ..., xn :
statistical n
programming?
2 1 X
The R package
Why use a s = (xi − x)2 ,
command line?
Installation of R
n − 1 i=1
and RStudio
Getting started

where x is the sample mean, n1 ni=1 xi . In R, s2 is available as


in RStudio P
Introduction
to the R
language
First steps
var(), and x mean().
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 82 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 83 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
Or
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 83 / 99
Lecture 1:
Introduction Because this formula requires calculation of x first and the sum of
Professor
Amany E. Aly squared deviations second, it requires that all xi values be kept in
Getting memory. Not too long ago memory was so expensive that it was
started
What is
statistical
advantageous to rewrite the formula as
programming?
The R package !
Why use a n
command line?
1 X
Installation of R
and RStudio s2 = x2i − n x2 ,
Getting started
in RStudio n − 1 i=1
Introduction
to the R
language
First steps
This is called the ”one-pass formula,” because we evaluate each
xi value just once, and accumulate the sums of xi and of x2i . It
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
gives the correct answer, both mathematically and in our example:
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 84 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 85 / 99
Lecture 1:
Introduction However, noticePwhat happens if we add a large value A to each
n
Professor
Amany E. Aly
2
xi . The sum i=1 xi increases by approximately n A2 , and so
Getting
does n x2 . This doesn’t change the variance, but it provides the
started
What is
statistical
conditions for a ”catastrophic loss of precision” when we take the
programming?
The R package
Why use a
difference:
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 86 / 99
Exact storage of numbers

Lecture 1:
Introduction

Professor
Amany E. Aly
R uses floating point storage for numbers, using a base 2 format
Getting
started that stores 53 bits of accuracy.
What is
statistical
programming?
The R package
It turns out that this format can store some fractions exactly: if
Why use a
command line?
Installation of R
the fraction can be written as n/2m, where n and m are integers
and RStudio
Getting started
in RStudio
(not too large; m can be no bigger than about 1000, but n can be
Introduction
to the R
very large), R can store it exactly. The number 5/4 is in this form,
language
First steps
but the number 4/5 is not, so only the former is stored exactly.
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 87 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
Floating point storage is not the only format that R uses. For
started
What is
whole numbers, it can use 32 bit integer storage. In this format,
statistical
programming?
The R package numbers are stored as binary versions of the integers 0 to 232 −1 =
Why use a
command line?
Installation of R
and RStudio
4294967295. Numbers that are bigger than 231 − 1 = 2147483647
Getting started
in RStudio are treated as negative values by subtracting 232 from them, i.e.
Introduction
to the R to find the stored value for a negative number, add 232 to it.
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 88 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly How does R decide which storage format to use? Generally, it
Getting does what you (or whoever wrote the function you’re using) tell
started
What is
statistical
it to do. If you want integer storage, append the letter L to the
programming?
The R package
Why use a
value: 11 means the floating point value, 11L means the integer
command line?
Installation of R
and RStudio value. Most R functions return floating point values, but a few
Getting started
in RStudio

Introduction
(e.g. seq(), which is used in expressions like 1:10) return integer
to the R
language values. Generally you don’t need to worry about this: values will
First steps
R can be used
as a calculator
be converted as needed.
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 89 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What about 64 bit integers? Modern computers can handle 64
What is
statistical
programming?
bits at a time, but in general, R can’t. The reason is that R
The R package
Why use a
command line?
expects integer values to be a subset of floating point values. Any
Installation of R
and RStudio
Getting started
32 bit integer can be stored exactly as a floating point value, but
in RStudio

Introduction
this is not true for all 64 bit integers.
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 90 / 99
Dates and times

Lecture 1:
Introduction

Professor
Amany E. Aly Dates and times are among the most difficult types of data to work
Getting
started
with on computers. The standard calendar is very complicated:
What is
statistical
programming?
months of different lengths, leap years every four years (with ex-
The R package
Why use a ceptions for whole centuries) and so on. When looking at dates
command line?
Installation of R
and RStudio over historical time periods, changes to the calendar (such as the
Getting started
in RStudio

Introduction
switch from the Julian calendar to the modern Gregorian calendar
to the R
language that occurred in various countries between 1582 and 1923) affect
First steps
R can be used
as a calculator
the interpretation of dates.
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 91 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
Times are also messy, because there is often an unstated time
statistical
programming?
The R package
zone (which may change for some dates due to daylight savings
Why use a
command line?
Installation of R
time), and some years have ”leap seconds” added in order to keep
and RStudio
Getting started
in RStudio
standard clocks consistent with the rotation of the earth.
Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 92 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly
There have been several attempts to deal with this in R. The
Getting
started base package has the function strptime() to convert from strings
What is
statistical
programming? (e.g. ”2020 − 12 − 25”, or ”12/25/20”) to an internal numer-
The R package
Why use a
command line? ical representation, and f ormat() to convert back for printing.
Installation of R
and RStudio
Getting started The ISOdate() and ISOdatetime() functions are used when nu-
in RStudio

Introduction merical values for the year, month, day, etc. are known. Other
to the R
language
First steps
functions are available in the chron package.
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 93 / 99
Missing values and other special values

Lecture 1:
Introduction

Professor
Amany E. Aly
The missing value symbol is N A. Missing values often arise in
Getting
started real data, but they can also arise because of the way calculations
What is
statistical
programming? are performed.
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 94 / 99
Lecture 1:
Introduction Consider the following:
Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio
The N aN symbol denotes a value which is ”not a number” which
Introduction
to the R arises as a result of attempting to compute the indeterminate 0/0.
language
First steps
R can be used
This symbol is sometimes used when a calculation does not make
as a calculator
Named storage
Quitting R
sense. In other cases, special values may be shown, or you may
Basic features of
R get an error or warning message.
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 95 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio Here R has tried to evaluate 1/0 and reports the infinite result as
Introduction
to the R Inf .
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 96 / 99
Lecture 1:
Introduction When there may be missing values, the is.na() function should
Professor
Amany E. Aly
be used to detect them.
Getting
started
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
The ! symbol means ”not,” so we can locate the non-missing
in RStudio

Introduction values in some.evens as follows:


to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 97 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
We can then display the even numbers only:
What is
statistical
programming?
The R package
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 98 / 99
Lecture 1:
Introduction

Professor
Amany E. Aly

Getting
started
What is
statistical
programming?
The R package Solve Exercises from 1 to 7 on pages 26 and 27.
Why use a
command line?
Installation of R
and RStudio
Getting started
in RStudio

Introduction
to the R
language
First steps
R can be used
as a calculator
Named storage
Quitting R
Basic features of
R
Functions
Professor
R is Amany E. Aly ( Department of Mathematics, Faculty of Science,
Lecture
Helwan
1: University,
IntroductionAin Helwan, Cairo, Egypt.) 30 / 9 / 2024 99 / 99

You might also like