0% found this document useful (0 votes)
11 views

00 Basics of R

This document provides an introduction to basic capabilities in R, including: - R can be used as a simple calculator for arithmetic operations and assigning values to variables. - Vectors can be created using the c() function and contain multiple values. Built-in functions like seq() can generate vectors. - Vectors can be indexed and individual elements accessed using numeric indices inside square brackets.

Uploaded by

dhphgvyie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

00 Basics of R

This document provides an introduction to basic capabilities in R, including: - R can be used as a simple calculator for arithmetic operations and assigning values to variables. - Vectors can be created using the c() function and contain multiple values. Built-in functions like seq() can generate vectors. - Vectors can be indexed and individual elements accessed using numeric indices inside square brackets.

Uploaded by

dhphgvyie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

20/02/2019 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.

Basic commands R is the implementation of the computer language S ,


and was created for statistical analysis. A brief summary
Vector operations may be found by clicking on the following web link to the
Wikipedia page for R
Functions (https://en.wikipedia.org/wiki/R_(programming_language)).

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

Basics of R Basic commands


One of the simplest uses of R is as a simple arithmetic
Introduction calculator as shown here.

Basic commands x <- 3


x
Vector operations

## [1] 3
Functions

R Packages sqrt(x)

Start Over
## [1] 1.732051

In this example a variable x has been declared and given


the value 3 . The third input line asks to calculate the
square root of x ; from this you can see that R has some
built in functions.

We can multiply two variables together, and assign the


result to a third:

y = 7
z = x * y
z

## [1] 21

The code above also illustrates that value can also be


assigned using the = sign. However, from now on and for
the sake of consistency, we will always use <- for
variable assignments.

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.

Introduction 1. Create a variable d a −



= √bc with a = 3 ,b = 2 ,
and c = 9 .
Basic commands
Code  Start Over  Hints  Run Code
Vector operations
1 a <- 3
2 b <- 2
Functions 3 c <- 9
4 # type down your codes below and click run codes
5 # click Hints for additional hints and solutions
R Packages

Start Over

2. Create a variable c = a
2 2
+ b with a = 3 ,b = 4

and check if c < 30.

Code  Start Over  Solution  Run Code

1 a <- 3
2 b <- 4
3 # type down your codes below and click run codes

Previous Topic Next Topic

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

Basics of R Vector operations


Vector creation
Introduction
We can also have arrays or vectors of values in R .
Basic commands Although there are several ways in which vector can be
created, one of the most common ones is to use the c
Vector operations function. For instance, the code below creates a vector of
3 values.
Functions

vec1 <- c(-3, 0, 2)


R Packages
vec1

Start Over
## [1] -3 0 2

Here, [1] states that the answer is starting at the first


element of a vector. When displaying a vector, R lists the
elements, from left to right, using (possibly) multiple rows
(depending on the width of the display). For example:

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

R provides a number of convenience functions for data


generation. See below how one can utilise the built-in R
functions to create vectors.

# 5 elements of 3
rep(3, 5)

## [1] 3 3 3 3 3

# first 10 positive intergers


seq(1, 10)

## [1] 1 2 3 4 5 6 7 8 9 10

# (1.0, 1.1, ..., 2.0)


seq(1, 2, 0.1)

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

# 6 equally spaced numbers between 1 and 2 (inc


Introduction lusive)
seq(1, 2, length = 6)
Basic commands

## [1] 1.0 1.2 1.4 1.6 1.8 2.0


Vector operations

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.

There are some more complicated ways to index vectors


in R . To illustrate this code below creates a vector
vec2 = (1, 3, 4, 10, 15, 20, 50, 1, 6) and performs
some operations:

# initialise the vector vec2


vec2 <- c(1, 3, 4, 10, 15, 20, 50, 1, 6)
vec2

## [1] 1 3 4 10 15 20 50 1 6

# extract the fifth, sixth and seventh elements


vec2[5:7]

## [1] 15 20 50

# extract the first, and seventh elements


vec2[c(1,7)]

## [1] 1 50

http://127.0.0.1:5544/#section-vector-operations 2/5
20/02/2019 Basics of R

# return a logical vector that indicates whethe


Basics of R r vec2>10
vec2 > 10

Introduction ## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRU


E FALSE FALSE
Basic commands

# determine the positions of elements of vec2 t


Vector operations
hat are larger than 10
which(vec2 > 10)
Functions

R Packages ## [1] 5 6 7

Start Over # extract the elements of vec2 that are larger


than 10
vec2[vec2>10]

## [1] 15 20 50

Applying functions to a vector


We can apply different functions to a vector. Using the
vector vec2 that has been created before, perform the
following operations

sqrt(vec2) # square root

## [1] 1.000000 1.732051 2.000000 3.162278 3.87


2983 4.472136 7.071068 1.000000
## [9] 2.449490

log(vec2) # natural logarithm

## [1] 0.000000 1.098612 1.386294 2.302585 2.70


8050 2.995732 3.912023 0.000000
## [9] 1.791759

length(vec2) # numeber of elements in a vector

## [1] 9

sum(vec2) # sum

## [1] 110

http://127.0.0.1:5544/#section-vector-operations 3/5
20/02/2019 Basics of R

diff(vec2) # difference between adjacent elemen


Basics of R ts

## [1] 2 1 6 5 5 30 -49 5
Introduction

Basic commands mean(vec2) # average

Vector operations ## [1] 12.22222

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. Extract the second and forth elements of the vector

Code  Start Over  Hints  Run Code

1
2
3

2. Compute the sum of the first five element of the


vector

Code  Start Over  Hints  Run Code

1
2
3

3. Extract the elements of vec2 that are smaller or


equal to 10

Code  Start Over  Hints  Run Code

1
2
3

Previous Topic Next Topic

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 <- function(x, p = 2){


x ^ p
}

We can then use this function in several ways:

pow(x = 2, p = 3)

## [1] 8

pow(2, 3)

## [1] 8

pow(1:10, 3)

## [1] 1 8 27 64 125 216 343 512


729 1000

Note that in the definition of the function p=2 means that


p is equal to 2 by default. Therefore pow(2, 2) returns
the same value as pow(2) .

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

Vector operations ## function(x, p = 2){


## x ^ p
## }
Functions
## <bytecode: 0x00000000288e2d68>

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 .

Code  Start Over  Hints  Run Code

1 j<-function(i){# note you need to specify the


2 # type down the body of the function here
3
4 }
5
6 # test the output
7 j
8
9 temp1 <- j()
10 temp1
11
12 temp2 <- j(0.07)

Previous Topic Next Topic

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/).

Functions To install a package from the Internet (for instance


lifecontingencies to perform financial and actuarial
R Packages
valuations) we use:

Start Over
install.packages("lifecontingencies", dependenc
ies=TRUE)

For the package to be available we need to load the


package in R with the command:

library("lifecontingencies")

We can now see the available functions in the package:

?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

You might also like