Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
45 views15 pages

Introduction To R For Finance: What Is A Data Frame?

This document introduces data frames in R. A data frame stores tabular data with rows and columns, like a spreadsheet. It demonstrates how to create a data frame from vectors, name the columns, and subset the data frame to extract specific rows or columns. It also covers time value of money concepts like future value, present value, and the general present value formula.

Uploaded by

nikhitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views15 pages

Introduction To R For Finance: What Is A Data Frame?

This document introduces data frames in R. A data frame stores tabular data with rows and columns, like a spreadsheet. It demonstrates how to create a data frame from vectors, name the columns, and subset the data frame to extract specific rows or columns. It also covers time value of money concepts like future value, present value, and the general present value formula.

Uploaded by

nikhitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

INTRODUCTION TO R FOR FINANCE

What is a data frame?


Introduction to R for Finance

Data frame
Column 1 Column 2 Column 3

Row 1 data 1 TRUE

Row 2 more data 2 TRUE

you really like


Row 3 3 TRUE
data

that's enough
Row 4 4 FALSE
data
Introduction to R for Finance

Data frames and friends


> name <- c("Dan", "Dan", "Dan", "Rob", "Rob", "Rob")
> payment <- c(100, 200, 150, 50, 75, 100)

> debt <- data.frame(name, payment)


> debt
name payment
1 Dan 100
2 Dan 200
3 Dan 150
4 Rob 50
5 Rob 75
6 Rob 100
Introduction to R for Finance

Name that frame!


> name <- c("Dan", "Dan", "Dan", "Rob", "Rob", "Rob")
> payment <- c(100, 200, 150, 50, 75, 100)
> debt <- data.frame(name, payment)

> colnames(debt) <- c("friend", “money”)

> debt
friend money
1 Dan 100
2 Dan 200
3 Dan 150
4 Rob 50
5 Rob 75
6 Rob 100

> debt <- data.frame(friend = name, money = payment)


INTRODUCTION TO R FOR FINANCE

Let’s practice!
INTRODUCTION TO R FOR FINANCE

Data frame manipulation


Introduction to R for Finance

Data frame subsets


> debt[3:6,]
name payment
3 Dan 150
4 Rob 50
5 Rob 75
6 Rob 100

> debt[1:3, 2]
[1] 100 200 150

> debt[1:3, 2, drop = FALSE]


payment
1 100
2 200
3 150

> debt$payment
[1] 100 200 150 50 75 100
Introduction to R for Finance

Subset() for more power


> # This works, but is not informative nor robust
> debt[1:3,]

> # Much more informative!


> subset(debt, name == "Dan")
name payment
1 Dan 100
2 Dan 200
3 Dan 150

> subset(debt, payment == 100)


name payment
1 Dan 100
6 Rob 100
INTRODUCTION TO R FOR FINANCE

Let’s practice!
INTRODUCTION TO R FOR FINANCE

Present value
Introduction to R for Finance

Time value of money


* 1.10

$100 $110
Today Future

$100
Introduction to R for Finance

Future value and present value


* 1.10 Future Value

$100 $110
Today Future

$90.91 $100

Present Value / 1.10 90.91 = 100 * (1.10) ^ -1


Introduction to R for Finance

Present value - multiple periods

82.64 = 100 * (1.10) ^ -2

Month Month
Today
1 2

$82.64 $90.91 $100

/ 1.10 / 1.10
Introduction to R for Finance

Present value - general formula


82.64 = 100 * (1.10) ^ -2
> present_value <- cash_flow * (1 + interest / 100) ^ -periods

> cash_flow <- 100


> interest <- 10
> periods <- 2

> present_value <- cash_flow * (1 + interest / 100) ^ -periods

> present_value
[1] 82.64463
INTRODUCTION TO R FOR FINANCE

Let’s practice!

You might also like