What are functions?

Intermediate R for Finance

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

Examples of functions

  • mean()
  • plot()
  • ncol()
Intermediate R for Finance

Elements of a function

  • Arguments
    • Input / data
    • Options
  • Body
    • Code execution
  • Return
    • Stable and predicable output
Intermediate R for Finance

Function documentation

?matrix

Screen Shot 2021-04-09 at 9.50.10 AM.png

Intermediate R for Finance

Function arguments

  • Required
    • Error thrown without it
    • Normally data / object
  • Optional
    • Default values are set
    • Normally sets extra options
Intermediate R for Finance

Function arguments example

returns <- c(.023, .044, .034, NA)

mean()
Error in mean.default() : argument "x" is missing, with no default
mean(returns)
NA
?mean

ch4_vid1_slides.021.png

mean(returns, na.rm = TRUE)
0.03366667
Intermediate R for Finance

Let's practice!

Intermediate R for Finance

Preparing Video For Download...