sapply() - simplify it!

Intermediate R for Finance

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

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"
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
Intermediate R for Finance

Let's practice!

Intermediate R for Finance

Preparing Video For Download...