Chapter 3 Functions in R
Most of what you do in R involves using functions. A function takes inputs (called arguments), performs an operation, and returns an output.
You have already used several functions, such as mean(), summary(), and seq().
[1] 23.5
Min. 1st Qu. Median Mean 3rd Qu. Max.
18.00 20.25 23.00 23.50 26.25 30.00
3.1 Function arguments
Functions often require one or more arguments. For example:
[1] 23.5
Here, ages is passed to the function mean() as its first argument.
Some functions require multiple arguments:
[1] 0.0 0.2 0.4 0.6 0.8 1.0
3.2 Positional vs named arguments
Arguments can be passed to a function in two ways.
3.2.1 Positional arguments
If arguments are supplied in the correct order, you do not need to name them.
[1] 0.0 0.2 0.4 0.6 0.8 1.0
This works because R knows the expected order of arguments for seq():
fromtoby
Positional arguments are concise, but they can make code harder to read and easier to misuse.
3.2.2 Named arguments (recommended)
You can explicitly name arguments using argument = value.
[1] 0.0 0.2 0.4 0.6 0.8 1.0
Advantages of named arguments:
- Code is easier to read
- Order does not matter
- Fewer mistakes when functions have many arguments
For example, this works even though the order is different:
[1] 0.0 0.2 0.4 0.6 0.8 1.0
In this course, naming arguments is recommended, especially for complex functions.
3.3 Default argument values
Many function arguments have default values. If you do not specify them, R uses the default.
Example:
[1] 23.5
The function mean() has optional arguments such as na.rm, which defaults to FALSE.
[1] 23.5
You only need to specify arguments when:
- You want a non-default behavior
- The function requires the argument
3.4 Viewing function documentation
To understand how a function works and what arguments it accepts, use the help system.
The help page shows:
- What the function does
- Required and optional arguments
- Default values
- Examples
When in doubt, read the argument list first.
3.5 Common mistakes to avoid
- Forgetting parentheses. This refers to the function itself, not the result.:
function (x, ...)
UseMethod("mean")
<bytecode: 0x000002b73dfeed18>
<environment: namespace:base>
- Misspelling argument names:
[1] 23.5
- Assuming argument order without checking documentation
3.6 Key takeaway
You do not need to memorize every function or argument. Instead, focus on:
- Understanding what a function expects as input
- Knowing when to use named arguments
- Reading help files when unsure
These habits will make your R code more readable, more reliable, and easier to debug.
3.7 What’s next
In the next chapter, we turn to working with course data.
You will learn how to:
- inspect and understand real datasets used in the course,
- distinguish between numeric and factor variables,
- identify missing or problematic values, and
- perform basic data manipulation using the
dplyrpackage.
These skills will allow you to move from isolated examples to analyzing complete datasets and will serve as the foundation for descriptive analysis, visualization, and modeling later in the semester.