R introduction

R studio http://www.rstudio.com

R introduction

Packages

# install.packages("tidyverse")
# library(tidyverse)

Basic operations

Excercise 1
Calculate the following operations:

2 + 5
12 - 15
3 * 7
16 / 4
4 ^ 2
23 %% 7
log(2.81)
sum(2, 5)
exp(5)
sqrt(256)
log(exp(3))
log10(1e7)

Calculate: the square of natural logarithm from the 2 to the power of 16

sqrt(log(2 ^ 16))

Logical operations

Excercise 2

Check whether: +\(\frac{1}{(1+0.03)^2}\) is grater than \(\frac{1}{(1+0.03/4)^8}\)

Note
Note the likely problem when using == floating point numbers.

sqrt(2) ^ 2 == 2
1 / 49 * 49 == 1

It is often better to use near() instead of ==

near(sqrt(2) ^ 2,  2)
near(1 / 49 * 49, 1)

Basic objects in R

Variables

var1 <- 144
var1
sqrt(var1)
var2 <- sqrt(var1)
var2
var3 <- sum(sqrt(var1), 8)
var3

Excercise 3

  1. Creat veriable called df and assigne the value of \(\frac{1}{(1+5.7\%)^4}\). Print the variable.
df <- 1 / (1 + 0.057) ^ 4
  1. Create variable ir with assigned value 5.7% and use this variable instead of explicitly declared value in point (b). Print the variable ir and df.
  2. Change the value of ir to 6.8% then print the variable ir and df.
ir <- 0.057
df <- 1 / (1 + ir) ^ 4

Basic objects in R

Data types

var1 <- 12.4
var1
position <- "Short"
var4 <- TRUE

Vector

Declaration

A vector is a sequence of data elements of the same basic type.

c(-50, 2, 3, 54)
c("a", "b", "c")
c(TRUE, FALSE, FALSE)

vector_cf <- c(-50, 2, 3, 54)
names(vector_cf) <- c("CF_1", "CF_2", "CF_3", "CF_4")

Vector

Operations

vector_cf[1]
vector_cf[4]
vector_cf[c(2, 4)]
vector_cf[c("CF_1", "CF_3")]
vector_cf_1 <- c(10, 10, 10, 12)
vector_cf_2 <- c(1, -2, 1, 0)
vector_cf + vector_cf_1 

Vector_cf_3 <- vector_cf + vector_cf_1 + vector_cf_2
log(vector_cf_1)
vector_cf_1 ^ 2
vector_cf_2 / vector_cf_1

Vector

Operations on a single vector

Excercise 4

  1. Claculate following statistics for the vector (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10):
sum(c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
# or declare a vector:
vector_1 <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sum(vector_1)
sd(vector_1)
min(vector_1)
max(vector_1)
  1. Check which is higher: mean or median, for the vector, which is a sum of vector (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) and its nalural logarithm?
vector_2 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + log(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
median(vector_2) > mean(vector_2)

Vector

Other usful operations

seq(from = 0, to = 10, by = 1) 
seq(0, 10, 1)
seq(-2, 12, 4)
seq(10, -10, -2)
rep(2, 12)

Excercise 5
1. Generate vector (10, 8, 6, 4, 2, 0, -2, -4, -6, -8, -10) using seq() function.

seq(10, -10, -2)
  1. Create a vector cf of cash payments: 10$ per year for next 9 years and 100 for the end of 10th year.
  2. Create a vector of years corresponding to cash flows in point 2.
cf <- c(rep(10, 9), 100)
year <- seq(1, 10, 1)
# year <- c(1:10)

Data frame

Data frame

Fundamental data structure, coupled collections of variables. How to create data frame containing our vectors?

data.frame(cf, year)
# create object data frame and assigne name cf_data
cf_data <- data.frame(cf, year)

Check the structure of data frame: str()
How to select data from data frame?

# 1st way
cf_data[1, 2]
cf_data[1, ]
cf_data[, 1]

# 2nd way
cf_data$cf
cf_data$year

cf_data$cf[10]

How to create new values in a data frame (table)?

# add vector cf_2 with values equal cf * 2:
cf_data$cf_2 <- 2 * cf_data$cf

Data frame

Excercise 6
Calculate present value of the cash flows: 8$ paid at the end of each year 1 till 10th, 12$ at the end of each year from 11 till 20th and additional 100$ at the end of year 20th. Discount rate is 6% for first 12 years and 7.5% for remaining period.

Hint:

\(PV = \frac{1}{(1 + r) ^ t}\)

Solution:

cf <- c(rep(8, 10), rep(12, 9), 112)
year <- seq(1, 20, 1)
r <- c(rep(0.06, 12), rep(0.075, 8))
cf_data <- data.frame(cf, year, r)

cf_data$pv_cf <- cf_data$cf / (1 + cf_data$r) ^ cf_data$year

sum(cf_data$pv_cf)