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

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

Introduction To R For Finance: What Is A Vector?

This document introduces vectors in R for finance applications. It discusses defining stock price data as vectors, naming the components of vectors, performing basic arithmetic operations on vectors including addition, subtraction, multiplication, and recycling values. It also covers creating and manipulating matrices, binding vectors together into matrices, and calculating the correlation between vectors. The overall purpose is to demonstrate basic vector and matrix handling skills needed for financial data analysis in R.

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)
53 views15 pages

Introduction To R For Finance: What Is A Vector?

This document introduces vectors in R for finance applications. It discusses defining stock price data as vectors, naming the components of vectors, performing basic arithmetic operations on vectors including addition, subtraction, multiplication, and recycling values. It also covers creating and manipulating matrices, binding vectors together into matrices, and calculating the correlation between vectors. The overall purpose is to demonstrate basic vector and matrix handling skills needed for financial data analysis in R.

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 vector?
Introduction to R for Finance

Vectors and stock prices


> apple <- 159.4

> apple_stock <- c(159.4, 160.3, 161.3)

> apple_stock
[1] 159.4 160.3 161.3

> is.vector(apple)
[1] TRUE

> grocery <- c("apple", "orange", "cereal")

> grocery
[1] "apple" "orange" "cereal"
Introduction to R for Finance

Vector names()
> apple_stock <- c(159.4, 160.3, 161.3)

> names(apple_stock) <- c("Monday", "Tuesday", "Wednesday")

> apple_stock
Monday Tuesday Wednesday
159.4 160.3 161.3
INTRODUCTION TO R FOR FINANCE

Let’s practice!
INTRODUCTION TO R FOR FINANCE

Vector manipulation
Introduction to R for Finance

Vectors and friends


> dan <- 100
> rob <- 50

> total <- dan + rob

> dan <- c(100, 200, 150)


> rob <- c(50, 75, 100)

> monthly_total <- dan + rob

> monthly_total
[1] 150 275 250

> sum(monthly_total)
[1] 675
Introduction to R for Finance

More examples
> a <- c(2.2, 12, 7)
> b <- c(11.5, 8, 3.4)

> # Subtraction!
> c <- a - b
> c
[1] -9.3 4.0 3.6

> # Multiplication!
> d <- a * b
> d
[1] 25.3 96.0 23.8

> # Recycling!
> e <- 2
> f <- a * e
> f
[1] 4.4 24.0 14.0
INTRODUCTION TO R FOR FINANCE

Let’s practice!
INTRODUCTION TO R FOR FINANCE

Matrix - a 2D vector
Introduction to R for Finance

Enter the matrix


> my_matrix <- matrix(c(2, 3, 4, 5), nrow = 2, ncol = 2)

> my_matrix
[,1] [,2]
[1,] 2 4
[2,] 3 5

> my_matrix2 <- matrix(c(2, 3, 4, 5), nrow = 2, ncol = 2,


byrow = TRUE)

> my_matrix2
[,1] [,2]
[1,] 2 3
[2,] 4 5
Introduction to R for Finance

Matrix coercion
> coerce_me <- matrix(c(2, 3, 4, "hi"), nrow = 2, ncol = 2)

> coerce_me
[,1] [,2]
[1,] "2" "4"
[2,] "3" "hi"
Introduction to R for Finance

cbind( ) and rbind( )


> micr <- c(59.20, 59.25, 60.22, 59.95)
> ebay <- c(17.44, 18.32, 19.11, 18.22)

> cbind(micr, ebay)


micr ebay
[1,] 59.20 17.44
[2,] 59.25 18.32
[3,] 60.22 19.11
[4,] 59.95 18.22

> rbind(micr, ebay)


[,1] [,2] [,3] [,4]
micr 59.20 59.25 60.22 59.95
ebay 17.44 18.32 19.11 18.22
Introduction to R for Finance

cor()relation
● +1: perfect positive linear relationship
● -1: perfect negative linear relationship
● 0: no linear relationship

.908
Introduction to R for Finance

cor()relation
> micr <- c(59.20, 59.25, 60.22, 59.95)
> ebay <- c(17.44, 18.32, 19.11, 18.22)

> cor(micr, ebay)


[1] 0.7835704

> micr_ebay_matrix <- cbind(micr, ebay)

> cor(micr_ebay_matrix)
micr ebay
micr 1.0000000 0.7835704
ebay 0.7835704 1.0000000
INTRODUCTION TO R FOR FINANCE

Let’s practice!

You might also like