Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 39 ยท Advanced R

Functional Programming in R: apply, Map, Reduce, Closures, purrr

Write cleaner R with functional programming: first-class functions, anonymous functions, lapply, sapply, vapply, Map, Filter, Reduce, closures, function factories and purrr.

R is a functional language at heart. Functions are ordinary values: you can store them in variables, pass them to other functions and return them from functions. This is the reason R programmers rarely write for loops for simple data jobs. Instead of "loop over the items and do X", they say "apply X to every item". The code gets shorter, loops no longer mutate state behind your back, and the intent is clearer.

R is not available on this site, so the code is read-only. Outputs shown are the standard results for these simple calls; a Python block at the end implements the same ideas so you can run and compare them.

Functions are values

R
square <- function(x) x^2
square(4)                        # 16

apply_twice <- function(f, x) f(f(x))
apply_twice(square, 3)           # 81   (3 squared is 9, squared again is 81)

Since R 4.1 you can write a short anonymous function with a backslash: \(x) x + 1 is the same as function(x) x + 1. It is perfect for one-off functions passed as arguments:

R
(\(x, y) x * y)(6, 7)            # 42

Operators are functions too. `+`(2, 3) returns 5, and sapply(1:3, -) works, because + and - are ordinary functions with funny names written in backticks.

The apply family

The *apply functions replace the common loop "do something for each element and collect the results". They differ in what they return.

Function Input Returns
lapply(X, f) vector or list list, always
sapply(X, f) vector or list vector or matrix if it can simplify, otherwise a list
vapply(X, f, FUN.VALUE) vector or list vector, with the type checked
Map(f, x, y) / mapply() several vectors list / simplified vector
apply(m, MARGIN, f) matrix per row (1) or column (2)
tapply(x, g, f) vector + grouping per-group result
R
sapply(1:5, \(x) x^2)                      # 1  4  9 16 25
lapply(1:3, \(x) x * 2)                    # a list of 3 numbers: 2, 4, 6
unlist(lapply(1:3, \(x) x * 2))            # 2 4 6
vapply(c("a", "bb", "ccc"), nchar, integer(1))
#   a  bb ccc 
#   1   2   3 

vapply() requires you to declare the shape of each result (integer(1): one integer). If a call returns something else, you get an error at once instead of a silently different type. That is why serious code prefers vapply() over sapply(), whose return type depends on the data (a list when the results have different lengths, a matrix when they all have the same length greater than one).

apply() works on matrices (and data frames converted to matrices):

R
m <- matrix(1:6, nrow = 2)        # rows: 1 3 5 / 2 4 6
apply(m, 1, sum)                  # 9 12   (sum of each row)
apply(m, 2, sum)                  # 3  7 11 (sum of each column)

For these two jobs rowSums() and colSums() are faster, but apply() handles any function.

Several inputs at once: Map and mapply

R
Map(function(x, y) x + y, 1:3, 4:6)       # list: 5, 7, 9
mapply(function(x, y) x + y, 1:3, 4:6)    # 5 7 9
mapply(rep, 1:3, 3:1)                     # list: 1 1 1 / 2 2 / 3

Map() is mapply() with a list result, and it pairs elements by position.

Filter, Reduce and friends

R
Filter(\(x) x %% 2 == 0, 1:10)            # 2 4 6 8 10
Position(\(x) x > 3, c(1, 5, 2, 7))       # 2      (index of the first match)
Find(\(x) x > 3, c(1, 5, 2, 7))           # 5      (the first match itself)

Reduce(`+`, 1:5)                          # 15
Reduce(`+`, 1:5, accumulate = TRUE)       # 1  3  6 10 15  (running totals)
Reduce(function(a, b) paste0(b, a), strsplit("abc", "")[[1]])   # "cba"

Reduce() folds a vector into one value by repeatedly applying a two-argument function: ((((1 + 2) + 3) + 4) + 5). It is the generalization of sum() and prod(), and it is how you combine a list of data frames (Reduce(merge, list_of_dfs)) or a list of sets (Reduce(intersect, list_of_vectors)).

do.call(f, args) calls a function with a list of arguments, the standard way to stack many pieces:

R
do.call(rbind, list(1:3, 4:6))
#      [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    4    5    6

Closures and function factories

A closure is a function that remembers the environment where it was created. Together with <<- (assign in the enclosing environment) it gives you private state:

R
make_counter <- function() {
  i <- 0
  function() {
    i <<- i + 1
    i
  }
}
c1 <- make_counter()
c1()      # 1
c1()      # 2
c2 <- make_counter()
c2()      # 1   (a separate counter with its own i)

A function factory is a function that returns customized functions:

R
power <- function(n) {
  force(n)                      # evaluate n now, not later (see below)
  function(x) x^n
}
square <- power(2)
cube   <- power(3)
square(4)    # 16
cube(2)      # 8

R evaluates arguments lazily, only when they are first used. Inside a loop that creates many closures this can bite, because every closure would see the final value of a loop variable; force(n) (or local()) captures the value at creation time.

Composing functions

R
compose <- function(f, g) function(x) g(f(x))
add_one_then_double <- compose(\(x) x + 1, \(x) x * 2)
add_one_then_double(5)     # 12

c(1, 4, 9) |> sqrt() |> sum()     # 6   (the native pipe feeds the left side to the right function)

The pipe |> (R 4.1+) turns nested calls sum(sqrt(c(1, 4, 9))) into a left-to-right sequence. The tidyverse offers the same with %>%.

purrr: the tidyverse way

The purrr package replaces the apply family with consistent, type-stable functions. The suffix tells you what comes back: map() a list, map_dbl() numbers, map_chr() strings, map_lgl() logicals, map_dfr() a stacked data frame.

R
library(purrr)

map_dbl(1:3, \(x) x / 2)             # 0.5 1.0 1.5
map_chr(c("a", "b"), toupper)        # "A" "B"
map2_dbl(1:3, 4:6, `*`)              # 4 10 18
keep(1:10, \(x) x > 7)               # 8 9 10
discard(1:10, \(x) x > 2)            # 1 2
reduce(1:4, `*`)                     # 24

Because the result type is fixed by the function name, errors show up immediately. purrr also helps with failure: safely(f) returns a version of f that never throws and returns list(result, error), so one bad input does not kill a batch of a thousand. pmap() iterates over rows of a data frame, and walk() is for side effects such as writing files.

Pure functions and why they help

A pure function returns the same output for the same input and changes nothing outside itself. Pure functions are easy to test, easy to run in parallel (parallel::mclapply(), furrr::future_map()) and easy to reason about. R's copy-on-modify semantics help: when you pass a vector to a function and modify it inside, the caller's vector is untouched.

R
double_all <- function(v) { v[] <- v * 2; v }
x <- c(1, 2, 3)
double_all(x)    # 2 4 6
x                # still 1 2 3

The same ideas in Python

Run this Python block to compare: map, filter, functools.reduce, closures with nonlocal and function factories look almost the same.

Python Python comparison: map, filter, reduce, closures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Using sapply() in package code and being surprised by a matrix or list. Use vapply() or purrr.
  • Writing a for loop that grows a vector with c(result, x); use vapply() or preallocate (see performance).
  • Using <<- for general state instead of returning values; it makes code hard to follow.
  • Forgetting force() when creating closures in a loop.
  • Passing a function call instead of a function: sapply(x, mean()) is wrong, sapply(x, mean) is right.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which apply function always returns a list?

    lapply() never simplifies. sapply() simplifies when it can, and vapply() returns a vector of the declared type.
  2. 2. What does Reduce(+, 1:5, accumulate = TRUE) return?

    With accumulate = TRUE you get every intermediate result: the running totals.
  3. 3. Why is vapply() safer than sapply()?

    vapply() checks that every result has the declared type and length.
  4. 4. What does the <<- operator do inside a closure?

    <<- searches the enclosing environments and modifies the variable it finds there, which gives closures persistent state.

Summary

  • Functions are values: pass them, return them and store them.
  • lapply(), sapply(), vapply(), Map(), apply() replace most loops; prefer type-stable vapply() or purrr's map_*().
  • Filter(), Reduce(), Position() and do.call() express selection and folding.
  • Closures keep private state; function factories return customized functions; use force() to capture arguments.
  • The native pipe |> and pure functions keep code readable and testable.

Next: Object-Oriented Programming in R: S3, S4 and R6