R studio http://www.rstudio.com
Packages
?function_name
# install.packages("tidyverse")
# library(tidyverse)
+
-
*
/
^
%%
log(x)
log10
, log2
, log(x, base)
sum(x)
exp(x)
sqrt(x)
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))
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)
Variables
<-
var1 <- 144
var1
sqrt(var1)
var2 <- sqrt(var1)
var2
var3 <- sum(sqrt(var1), 8)
var3
Excercise 3
df
and assigne the value of \(\frac{1}{(1+5.7\%)^4}\). Print the variable.df <- 1 / (1 + 0.057) ^ 4
ir
with assigned value 5.7% and use this variable instead of explicitly declared value in point (b). Print the variable ir
and df
.ir
to 6.8% then print the variable ir
and df
.ir <- 0.057
df <- 1 / (1 + ir) ^ 4
Data types
12.4
"Low"
TRUE
, FALSE
class()
var1 <- 12.4
var1
position <- "Short"
var4 <- TRUE
Declaration
A vector is a sequence of data elements of the same basic type.
numeric_vector: -50, 2, 3, 54
character_vector: "a", "b", "c"
boolean_vector: TRUE, FALSE, FALSE
To declare a vector we use c
function
To assigne name to vector object use <-
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")
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
Operations on a single vector
sum()
: Calculate the sum of all the values in a data structuremean()
: Calculate the arithmetic meanmedian()
: Compute the sample mediansd()
: Computes the standard deviationmax()
: Returns the mximummin()
: Returns the minimumabs()
: Calculate the absolute valuerev()
: Reverse the elements in a data structures for which reversal is definedsort()
: Sort a vector in ascending orderc()
: Combine values into a vector or listappend()
: Merge vectors or listsExcercise 4
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)
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)
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)
cf
of cash payments: 10$ per year for next 9 years and 100 for the end of 10th year.cf <- c(rep(10, 9), 100)
year <- seq(1, 10, 1)
# year <- c(1:10)
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
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:
cf
containting cash flowsr
containing ratesyear
containing consecutive period numbers from 1 till 20cf_data
using data.frame
function (print the cf_data
to get familiar with the structure of the data)cf_data$persent_value_cf <- ...here type the formula for PV...
using the relevent vector names from cf_data
like cf_data$cf
instead of declaring values explicitly;)\(PV = \frac{1}{(1 + r) ^ t}\)
124.2177
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)