You are on page 1of 6

Intro for R for Finance

Emily Atkins
6/14/2017

 Intro for R for Finance


o Chapter 1
 Arithmetic in R (1)
 Assignment and variables
 Assignment and variables 2
 Financial returns
 Financial Returns 2
 Data type exploration
 What’s that data type?
o Chapter 2
 C()ombine
 Coerce It
 Vector Names
 Visualizing your vector
 Weighted Average
 Weighted Average 2
 Weighted average 3
 Vector Subsetting
 Create your Matrix
 Matrix <- bind vectors
 Visualize your Matrix
 Correlation
 Matrix Subsetting
o Chapter 3
 Creating a data frame
 Making head()s and tail()s of your data with some str()ucture
 Naming your columns / rows
 Accessing and subsetting data frames
 Accessing and subsetting data frames 2
 Accessing data frames and subsettings 3
 Adding new columns
 Present value of projected cash flows
 Present value of projected cash flows 2
o Chapter 4
 Creating a vector
 Factor Levels
 Factor summary
 Visualize your factor
 Bucketing a numeric variable into a factor
 Create an ordered factor
 Subsetting a factor
 stringsAsFactors
o Chapter 5
 Create a list
 Named lists
 Accessing elements in a list
 Adding to a list
 Split-Apply-Combine
 Removing from a list
 Spliting a list
 Attributes
o QUIZ

Intro for R for Finance


Chapter 1
Throughout this chapter I learned about the basics of R. First understanding simple addition
and subtraction, then figuring out different variables, calculating financial returns and
determinging different data types. ### First R Script Must remember to use the order of
operations when conducting these

# Addition!
3 + 5
## [1] 8
## [1] 8

# Subtraction!
6-4
## [1] 2
## [1] 2

Arithmetic in R (1)
# Addition
2 + 2
## [1] 4
# Subtraction
4 - 1
## [1] 3

# Multiplication
3 * 4
## [1] 12

# Division
4 /2
## [1] 2
# Exponentiation
2^4
## [1] 16

# Modulo
7 %% 3
## [1] 1

Assignment and variables


# Assign 200 to savings
savings <- 200

# Print the value of savings to the console


savings
## [1] 200
## [1] 200

Assignment and variables 2


We then looked at “Real life” possibile situations like money problems and how to calculate
how much people owed you.

# Assign 100 to my_money


my_money <- 100

# Assign 200 to dans_money


dans_money <- 200

# Add my_money and dans_money


my_money + dans_money
## [1] 300

# Add my_money and dans_money again, save the result to our_money


our_money <- 300

Financial returns
We learned about the multiplier equation which is : $multiplier = 1 + (return / 100)

# Variables for starting_cash and 5% return during January


starting_cash <- 200
jan_ret <- 5 # 5% interest rate
jan_mult <- 1 + (jan_ret / 100)

# How much money do you have at the end of January?


post_jan_cash <- starting_cash * jan_mult

# Print post_jan_cash
post_jan_cash
## [1] 210
## [1] 210

# January 10% return multiplier


jan_ret_10 <- 10
jan_mult_10 <- 1 + 10 / 100

# How much money do you have at the end of January now?


post_jan_cash_10 <- starting_cash * jan_mult_10

# Print post_jan_cash_10
post_jan_cash_10
## [1] 220
## [1] 220

Financial Returns 2
# Starting cash and returns
starting_cash <- 200
jan_ret <- 4 # 4% interest rate
feb_ret <- 5

# Multipliers
jan_mult <- 1 + 4 / 100
feb_mult <- 1 + 5 / 100
# Total cash at the end of the two months
total_cash <- starting_cash * jan_mult * feb_mult

# Print total_cash
total_cash
## [1] 218.4
## [1] 218.4

Data type exploration


R’s most basic data types are numerics, logicals and characters

# Apple's stock price is a numeric


apple_stock <- 150.45

# Bond credit ratings are characters


credit_rating <- "AAA"

# You like the stock market. TRUE or FALSE?


my_answer <- TRUE

# Print my_answer
my_answer
## [1] TRUE
## [1] TRUE

What’s that data type?


a <- TRUE
class(a)
## [1] "logical"
## [1] "logical"

b <- 5.5
class(b)
## [1] "numeric"
## [1] "numeric"
c <- "Hello World"
class(c)
## [1] "character"
## [1] "character"

You might also like