Chapter 3 Libraries and Functions

R comes with many built-in functions, but its real power lies in the thousands of user-contributed packages available for specialized tasks. This chapter covers how to use functions and extend R’s capabilities with libraries.

3.1 Functions

A function takes inputs, performs an operation, and returns an output. You’ve already used several functions like c(), mean(), and data.frame(). Functions are called by their name followed by parentheses containing any arguments:

# Function with one argument
sqrt(16)
## [1] 4
# Function with multiple arguments
round(3.14159, digits = 2)
## [1] 3.14
# Function with no arguments
Sys.Date()
## [1] "2026-01-28"

3.1.1 Base R Functions

R includes hundreds of built-in functions for common tasks. These are available immediately without loading any packages:

# Mathematical functions
sum(1, 2, 3, 4, 5)
## [1] 15
abs(-42)
## [1] 42
log(100)
## [1] 4.60517
exp(2)
## [1] 7.389056
# Statistical functions
x <- c(12, 15, 18, 22, 25, 30)
mean(x)
## [1] 20.33333
median(x)
## [1] 20
sd(x)
## [1] 6.65332
var(x)
## [1] 44.26667
min(x)
## [1] 12
max(x)
## [1] 30
range(x)
## [1] 12 30

3.1.2 Getting Help

To learn what a function does and what arguments it accepts, use the help system:

# Open help page
?mean
help(mean)

# Search for functions by keyword
??regression

The help page shows the function’s arguments, what it returns, and usually includes examples at the bottom.

3.2 Installing Packages

Packages extend R with new functions and datasets. You install a package once using install.packages():

install.packages("stringr")

To install multiple packages at once:

install.packages(c("dplyr", "ggplot2", "tidyr"))

Packages download from CRAN, a repository of reviewed R packages. You only need to install a package once per computer.

3.3 Loading Packages

Installing puts a package on your computer, but you must load it in each R session to use its functions. Use library():

library(stringr)

A common error is calling a function before loading its package:

Error: could not find function "str_to_upper"

This means you need to run library(stringr) first.

3.4 Calling Functions from Packages

3.4.1 After Loading

Once a package is loaded, you can call its functions directly:

library(stringr)
str_to_upper("hello world")

3.4.2 Without Loading

You can also call a function without loading the entire package using the :: operator:

stringr::str_to_upper("hello world")

This syntax is useful when:

  • You only need one function from a package
  • Two packages have functions with the same name
  • You want to make clear which package a function comes from

3.4.3 Function Conflicts

Sometimes two packages have functions with the same name. The package loaded last takes priority. To use the other version, use the :: operator:

# Both dplyr and stats have a filter() function
library(dplyr)
library(stats)

# This uses stats::filter (loaded last)
filter(x)

# To explicitly use dplyr's filter
dplyr::filter(data, column > 5)

3.5 Packages Used in This Book

Package Purpose
tidyverse Data manipulation and visualization
readxl Read Excel files
haven Read Stata, SPSS, and SAS files
WDI World Bank data
fredr Federal Reserve (FRED) data

3.6 Exercises

  1. Use the ? operator to look up the help page for the seq() function. Use it to create a sequence from 0 to 100 by increments of 5.

  2. Install the haven package. Then use the :: operator to check what functions it contains by typing haven:: and pressing Tab in RStudio.

  3. Calculate the natural log of 50 using log(), then verify your answer using exp().