Chapter 13 Functions

Learning Objectives

After completing this chapter, you will be able to:

  • Define custom functions with arguments and default values
  • Understand variable scoping and how R searches for variables
  • Write anonymous functions for concise, one-off operations
  • Use ... (dot-dot-dot) to pass additional arguments flexibly

In theory, you can already write very complicated codes, accomplishing a lot of data analysis tasks. In this chapter, you will learn a very powerful tool, functions.

At a glance – Chapter ROADMAP

Section 13.1. Defining Functions: Learn to create custom, reusable blocks of code with arguments and return values.
Section 13.2 & Section 13.3. Advanced Concepts: Master anonymous functions, nested functions, and understand variable scoping rules in R.
Section 13.4. Functional Programming: Use the apply family and purrr for cleaner, more efficient iteration over lists and data frames.
Section 13.5. Robustness: Implement tryCatch() and other tools to handle errors and warnings gracefully.


Buy Me A Coffee