Why use apply? Lore Dirick Instructor, DataCamp Intermediate R for - - PowerPoint PPT Presentation

why use apply
SMART_READER_LITE
LIVE PREVIEW

Why use apply? Lore Dirick Instructor, DataCamp Intermediate R for - - PowerPoint PPT Presentation

INTERMEDIATE R FOR FINANCE Why use apply? Lore Dirick Instructor, DataCamp Intermediate R for Finance Meet the apply family Function Description apply Apply functions over array margins lapply Apply a function over a list or vector


slide-1
SLIDE 1

INTERMEDIATE R FOR FINANCE

Why use apply?

Lore Dirick

Instructor, DataCamp

slide-2
SLIDE 2

Intermediate R for Finance

Meet the apply family

Function Description apply Apply functions over array margins lapply Apply a function over a list or vector eapply Apply a function over values in an environment mapply Apply a function to multiple lists or vector arguments rapply Recursively apply a function to a list tapply Apply a function over a ragged array sapply Simplify the result from lapply vapply Strictly simplify the result from lapply

slide-3
SLIDE 3

Intermediate R for Finance

lapply()

> stock_list <- list(stock_name = "Apple", ticker = "AAPL", price = 126.5, good_deal = TRUE) > lapply(stock_list, FUN = class) $stock_name [1] "character" $ticker [1] "character" $price [1] "numeric" $good_deal [1] "logical"

slide-4
SLIDE 4

Intermediate R for Finance

Break it down

stock_list lapply() $stock_name "Apple" $ticker "AAPL" $price 126.5 $good_deal TRUE "character" "numeric" "logical" class() class() class() class() "character" $stock_name "character" $ticker "character" $price "numeric" $good_deal "logical"

slide-5
SLIDE 5

Intermediate R for Finance

Sharpe ratio

sharpe = mean(r) − rf sd(r)

  • Normalize returns by risk
  • Compare returns among stocks
  • Higher sharpe ratio = More return / unit risk
slide-6
SLIDE 6

INTERMEDIATE R FOR FINANCE

Let’s practice!

slide-7
SLIDE 7

INTERMEDIATE R FOR FINANCE

sapply() - simplify it!

slide-8
SLIDE 8

Intermediate R for Finance

sapply()

> stock_list <- list(stock_name = "Apple", ticker = "AAPL", price = 126.5, good_deal = TRUE) > sapply(stock_list, FUN = class) stock_name ticker price good_deal "character" "character" "numeric" "logical"

slide-9
SLIDE 9

Intermediate R for Finance

Apply a custom summary function

> simple_summary <- function(x) { c(mean = mean(x), sd = sd(x)) } > head(stock_return, 3) apple ibm micr 1 0.003744634 0.001251408 0.0008445946 2 -0.007188353 -0.001124859 0.0163713080 3 0.007698653 0.003190691 -0.0044835603 > sapply(stock_return, FUN = simple_summary) apple ibm micr mean 0.002838389 0.001926806 0.002472939 sd 0.007157457 0.008130703 0.009943938

slide-10
SLIDE 10

INTERMEDIATE R FOR FINANCE

Let’s practice!

slide-11
SLIDE 11

INTERMEDIATE R FOR FINANCE

vapply() - specify

your output!

slide-12
SLIDE 12

Intermediate R for Finance

vapply()

> args(vapply) function (X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE) NULL > vapply(stock_list, FUN = class, FUN.VALUE = character(1)) stock_name ticker price good_deal "character" "character" "numeric" "logical"

slide-13
SLIDE 13

Intermediate R for Finance

Anonymous functions

> vapply(stock_return, FUN = function(x) {c(mean = mean(x), sd = sd(x))}, FUN.VALUE = numeric(2)) apple ibm micr mean 0.002838389 0.001926806 0.002472939 sd 0.007157457 0.008130703 0.009943938

slide-14
SLIDE 14

INTERMEDIATE R FOR FINANCE

Let’s practice!