Chapter 11 Functions

ds4psy: (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, 2021), various tidyverse packages (Wickham et al., 2019), or the ds4psy package (Neth, 2022). An important next step is learning to program, which essentially consists in creating our own functions.

Writing functions changes how you interact with R<br> and is a key step in learning to program.

Figure 11.1: Writing functions changes how you interact with R
and is a key step in learning to program.

Writing your own functions will change your 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 you well for quite a while, but eventually you 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 you to address a wide variety of tasks. And just as you usually feel no need to design a new hammer whenever you 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 your analysis needs get more specific and demanding, acquiring a general skill (like programming) can be more useful than always searching for another tool that may meet your current need (i.e., locating functions or packages that solve particular problems).

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.

References

Neth, H. (2022). ds4psy: Data science for psychologists. Retrieved from https://CRAN.R-project.org/package=ds4psy
R Core Team. (2021). R base: A language and environment for statistical computing. Retrieved from https://www.R-project.org
Wickham, H., Averick, M., Bryan, J., Chang, W., McGowan, L. D., François, R., … Yutani, H. (2019). Welcome to the tidyverse. Journal of Open Source Software, 4(43), 1686. https://doi.org/10.21105/joss.01686