This chapter did not discuss any new tidyverse package, but rather two basic notions of computer programming:
A function is defines a particular mapping from inputs to outputs. When creating more complex programs, defining functions allows us to abstract procedures that we may want to use repeatedly.
Writing functions involves controlling the flow of information, both between and within functions. Conditional statements (aka. if-then statments) define criteria for distinguishing cases and then use them for pursuing alternative paths in our programs.
After working through this chapter, you are able to
- explain what functions are and why they are useful,
- use base R to define new functions,
- describe and check functions,
- control the flow of information by using conditional statements,
- recognize some advanced issues (like recursion, sorting algorithms, and measuring the performance of functions).
Take a look at the RStudio cheatsheets on Base R and Advanced R on Functions to check which commands you are now familiar with and which others you can still discover in the future:
Let’s test the functionality of our knowledge and skills on programming functions by completing the following exercises.