00 Basics of R
00 Basics of R
Basics of R Introduction
In this tutorial we aim to provide an introduction to the
Introduction basic capabilities of the R software.
R Packages If you are already familiar with R feel free to skip this
tutorial and go directly to the tutorial on Life tables with the
Start Over
package lifecontingencies .
Next Topic
http://127.0.0.1:5544/ 1/1
20/02/2019 Basics of R
## [1] 3
Functions
R Packages sqrt(x)
Start Over
## [1] 1.732051
y = 7
z = x * y
z
## [1] 21
To see if two variable have the same value, you use the
double equals (with no space between them) == . Logical
expressions like x == y can take TRUE or FALSE values.
x == y
## [1] FALSE
x == y - 2
## [1] FALSE
http://127.0.0.1:5544/#section-basic-commands 1/3
20/02/2019 Basics of R
Exercises
Basics of R Solve the following exercises using the code chunks
below.
Start Over
2. Create a variable c = a
2 2
+ b with a = 3 ,b = 4
1 a <- 3
2 b <- 4
3 # type down your codes below and click run codes
http://127.0.0.1:5544/#section-basic-commands 2/3
20/02/2019 Basics of R
Basics of R
Introduction
Basic commands
Vector operations
Functions
R Packages
Start Over
http://127.0.0.1:5544/#section-basic-commands 3/3
20/02/2019 Basics of R
Start Over
## [1] -3 0 2
u <- 1:50
u
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50
# 5 elements of 3
rep(3, 5)
## [1] 3 3 3 3 3
## [1] 1 2 3 4 5 6 7 8 9 10
http://127.0.0.1:5544/#section-vector-operations 1/5
20/02/2019 Basics of R
## [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
Basics of R 2.0
Functions
Indexing vectors
R Packages
We can index vectors in several ways. For instance the
Start Over code below illustrates some basic indexing operations.
vec1[1]
## [1] -3
vec1[-1]
## [1] 0 2
The first line extracts the first element of the vector while
the second line all but the first element of the vector.
## [1] 1 3 4 10 15 20 50 1 6
## [1] 15 20 50
## [1] 1 50
http://127.0.0.1:5544/#section-vector-operations 2/5
20/02/2019 Basics of R
R Packages ## [1] 5 6 7
## [1] 15 20 50
## [1] 9
sum(vec2) # sum
## [1] 110
http://127.0.0.1:5544/#section-vector-operations 3/5
20/02/2019 Basics of R
## [1] 2 1 6 5 5 30 -49 5
Introduction
Functions
R Packages Exercises
Use the vector vec2 that has been created before to
Start Over
perform the following operations.
vec2
## [1] 1 3 4 10 15 20 50 1 6
1
2
3
1
2
3
1
2
3
http://127.0.0.1:5544/#section-vector-operations 4/5
20/02/2019 Basics of R
Basics of R
Introduction
Basic commands
Vector operations
Functions
R Packages
Start Over
http://127.0.0.1:5544/#section-vector-operations 5/5
20/02/2019 Basics of R
Basics of R Functions
We have used so far several R functions. If you are
Introduction unsure about what a particular function does you can get
some help using either help or ? . For instance,
Basic commands
?sd
Vector operations help(sd)
Functions will display a help page for the sd function, with details
on the input values, the output, and some examples.
R Packages
However, often there is no function to compute what we
Start Over
need, so we have to write our own functions. For instance,
a function to compute the p -th power of x can be defined
as follows:
pow(x = 2, p = 3)
## [1] 8
pow(2, 3)
## [1] 8
pow(1:10, 3)
pow(2, 2)
## [1] 4
pow(2)
http://127.0.0.1:5544/#section-functions 1/3
20/02/2019 Basics of R
## [1] 4
Basics of R
If we type pow in the console, the code of the function will
appear:
Introduction
pow
Basic commands
R Packages
Exercise
Start Over
Now complete the following code to create a function that
calculates the effective annual interest rate, i , given the
nominal annual interest rate (monthly compounding), j .
The default value is i = 0.05 . The formula is
j = (i/12)
12
− 1 .
http://127.0.0.1:5544/#section-functions 2/3
20/02/2019 Basics of R
Basics of R
Introduction
Basic commands
Vector operations
Functions
R Packages
Start Over
http://127.0.0.1:5544/#section-functions 3/3
20/02/2019 Basics of R
Basics of R R Packages
A package is a related set of functions, including help files
Introduction and data files, that have been bundled together and is
shared among the R community. There are thousands of
Basic commands packages available covering different disciplines. A useful
start for searching packages on a particular topic is CRAN
Vector operations
Task Views (https://cran.r-project.org/web/views/).
Start Over
install.packages("lifecontingencies", dependenc
ies=TRUE)
library("lifecontingencies")
?lifecontingencies
Previous Topic
http://127.0.0.1:5544/#section-r-packages 1/2
20/02/2019 Basics of R
Basics of R
Introduction
Basic commands
Vector operations
Functions
R Packages
Start Over
http://127.0.0.1:5544/#section-r-packages 2/2