What is a factor?

Introduction to R for Finance

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

Stocks or bonds?

Investment
stock
bond
bond
stock
Introduction to R for Finance

Stocks or bonds?

Investment
stock = 2
bond = 1
bond = 1
stock = 2
Introduction to R for Finance

Stocks or bonds?

Investment
stock
bond
bond
stock
Introduction to R for Finance

Factor creator

answers <- c("stock", "bond", "bond", "stock")
investment <- factor(answers)
investment
stock bond  bond  stock
Levels: bond stock
class(investment)
"factor"
Introduction to R for Finance

Factor creator

as.integer(investment)
2 1 1 2
levels(investment)
"bond"  "stock"
Introduction to R for Finance

cut() it up

  • Ranking: 1-50
  • 1 is worst, 50 is best
head(ranking)
36 45 32 10 42  8
buckets <- c(0, 10, 20, 30, 40, 50)
ranking_grouped <- cut(ranking, breaks = buckets)
head(ranking_grouped)
(30,40] (40,50] (30,40] (0,10]  (40,50] (0,10] 
Levels: (0,10] (10,20] (20,30] (30,40] (40,50]
Introduction to R for Finance

Let's practice!

Introduction to R for Finance

Preparing Video For Download...