Chapter 11 Functions
In the previous chapters, we have been using all kinds of functions to solve specific tasks. For instance, we have learned to load, explore, and transform data tables into various forms and formats. To achieve this, we have used many different functions from base R (R Core Team, 2024), various tidyverse packages (Wickham et al., 2019), or the ds4psy package (Neth, 2023). An important next step is learning to program, which essentially consists in creating our own functions.
Writing our own functions will change our way of interacting with R. To put this step into perspective, consider the old proverb:
Give people a fish, and you feed them for a day.
Teach people to fish, and you feed them for a lifetime.
When applied to R, this means that the existing R functions and packages may serve us well for quite a while, but eventually we will want to write your own functions. As most analogies, likening learning R to fishing to feed oneself has its limitations. As we discussed in the introductory chapter, R provides a toolbox and comes with many tools on different levels of abstraction. Thus, learning to use the rich collection of existing functions is quite an accomplishment and enables us to address a wide variety of tasks. And just as we usually feel no need to design a new hammer whenever we want to hit a nail, there is nothing wrong with using existing functions, as long as they get the job done. In the long run, however, and as our analysis needs get more specific and demanding, acquiring a general skill (like programming a new function) can be more useful than always searching for a tool that may or may not meet our current needs (i.e., locating functions or packages that solve particular problems). Thus, a key element of programming consists in writing our own functions.
In this chapter, we first introduce the basics of creating new functions (in Section 11.2). As this frequently requires checking cases and controlling the flow of information in a new function, we will also learn how to use conditional statements (in Section 11.3). The final section on Advanced aspects (Section 11.4) introduces programming techniques known as recursion, popular solutions to the task of sorting, and basic ways of measuring the performance of functions. Some exposure to these topics and techniques is relevant and useful in any programming language, not just in R. Together with Chapter 12 on Iteration, this knowledge will enable us to create programs that allow abstracting and automating complex procedures.