Open In App

Purrr Package in R Programming

Last Updated : 23 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Purrr package in R Programming , provides a set of functions that are designed to work with functional programming concepts, such as mapping, filtering and reducing. These functions are designed to work with lists, data frames and other objects, making it easier to work with complex data structures.

Functions Available in purrr Package

Here are some of the functions available in the purrr package.

Function

Description

pluck()By passing the indices of the element to be extracted, this method extracts a single element from a nested list.
map_dbl()This function is comparable to map() but instead of returning a list, it produces a numeric vector. It does this by applying a specified function to each element of a list and then returning a vector with the same length as the input list that contains the outcomes of doing so.
map_df()When a data frame with the same number of rows as the input list and columns holding the results of applying the function to each element is returned, it has applied the specified function to each element of the input list.
map_chr()It does this by applying a specified function to each element of a list and then returning a vector with the same length as the input list that contains the outcomes of doing so.
pmap()Each input list's corresponding components are subject to the application of a specified function and a list with the same length as the input lists is returned.
transpose()The first element of each sub-list becomes the first element of the output list when using this function to transform a list of lists. The second element of each sub-list becomes the second element of the output list and so on. Working with material that has been organised as a list of lists can benefit from this.
keep()This function uses a predicate function to filter a list's components. Only the components for which the predicate function returns TRUE are included in the new list that is returned.
accumulate()Similar to reduce(), this function gives a list of accumulated values as opposed to a single accumulated value.
some()This function determines whether a list's minimum number of items satisfy a specified predicate function. If at least one element matches the predicate function, it returns TRUE; otherwise, it returns FALSE.

Installation and Loading

To install the purrr package and load it into our R environment , we will use the install.packages() and library() function.

R
install.packages("purrr")
library(purr)

Simplifying Iterations

Iteration is another core concept in functional programming. It involves applying a function to a set of inputs, one at a time and returning a set of outputs. The purrr package provides a more efficient way to perform iteration using the map() and walk() functions. 

1. Iteration using map( ) function

The map() function is used to apply a function to each element of a list or a vector. It takes a function and a list or a vector as inputs and returns a new list or vector where the function has been applied to each element of the original list or vector. The output type will be the same as the input type. T

R
numbers <- c(1, 2, 3, 4, 5)

map(numbers, function(x) x^2)

Output:

> map(numbers, function(x) x^2)
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

R
list_of_dfs <- list(
  data.frame(a = c(1, 2, 3), b = c(4, 5, 6)),
  data.frame(a = c(7, 8, 9), b = c(10, 11, 12)),
  data.frame(a = c(13, 14, 15), b = c(16, 17, 18))
)

map(list_of_dfs, function(df) summary(df))

Output:

> map(list_of_dfs, function(df) summary(df))
[[1]]
a b
Min. :1.0 Min. :4.0
1st Qu.:1.5 1st Qu.:4.5
Median :2.0 Median:5.0
Mean :2.0 Mean :5.0
3rd Qu.:2.5 3rd Qu.:5.5
Max. :3.0 Max. :6.0

[[2]]
a b
Min. :7.0 Min. :10.0
1st Qu.:7.5 1st Qu.:10.5
Median :8.0 Median :11.0
Mean :8.0 Mean :11.0
3rd Qu.:8.5 3rd Qu.:11.5
Max. :9.0 Max. :12.0

[[3]]
a b
Min. :13.0 Min. :16.0
1st Qu.:13.5 1st Qu.:16.5
Median :14.0 Median :17.0
Mean :14.0 Mean :17.0
3rd Qu.:14.5 3rd Qu.:17.5
Max. :15.0 Max. :18.0

2. Iteration using walk( ) function

The walk() function is used to apply a function to each element of a list or a vector, but it does not return anything. Instead, it is used when we want to perform an operation on each element of a list or a vector, such as printing or saving to a file. 

R
library(purrr)

df <- data.frame(
  x = c(1, 2, 3, 4),
  y = c(5, 6, 7, 8)
)

walk(df$y, ~ print(.x * 3))

Output:

> walk(df$y, ~ print(.x * 3))
[1] 15
[1] 18
[1] 21
[1] 24

Functional Programming Tools

Purrr also provides a set of functional programming tools, such as reduce(), accumulate(), compose(), partial(), etc., that make it easier to work with functions.

1. reduce( ) function

The reduce() function is used to successively apply a binary function to the elements of a vector, list or data frame and returns a single value.

R
library(dplyr)
library(purrr)

data("iris")
long_sepals <- filter(iris, Sepal.Length > 5)
petal_sum <- reduce(long_sepals$Petal.Length, `+`)

petal_sum

Output:

> petal_sum
[1] 509.2

2. accumulate( ) function

The accumulate() function allows to apply a function iteratively to each element of a vector or list, accumulating the results at each step.

R
library(purrr)

iris_sum <- iris %>% 
  pull(Sepal.Length) %>% 
  accumulate(`+`)

print(iris_sum)

Output:

> print(iris_sum)
[1] 5.1 10.0 14.7 19.3 24.3 29.7 34.3 39.3 43.7 48.6 54.0 58.8 63.6
[14] 67.9 73.7 79.4 84.8 89.9 95.6 100.7 106.1 111.2 115.8 120.9 125.7 130.7
[27] 135.7 140.9 146.1 150.8 155.6 161.0 166.2 171.7 176.6 181.6 187.1 192.0 196.4
[40] 201.5 206.5 211.0 215.4 220.4 225.5 230.3 235.4 240.0 245.3 250.3 257.3 263.7
[53] 270.6 276.1 282.6 288.3 294.6 299.5 306.1 311.3 316.3 322.2 328.2 334.3 339.9
[66] 346.6 352.2 358.0 364.2 369.8 375.7 381.8 388.1 394.2 400.6 407.2 414.0 420.7
[79] 426.7 432.4 437.9 443.4 449.2 455.2 460.6 466.6 473.3 479.6 485.2 490.7 496.2
[92] 502.3 508.1 513.1 518.7 524.4 530.1 536.3 541.4 547.1 553.4 559.2 566.3 572.6
[105] 579.1 586.7 591.6 598.9 605.6 612.8 619.3 625.7 632.5 638.2 644.0 650.4 656.9
[118] 664.6 672.3 678.3 685.2 690.8 698.5 704.8 711.5 718.7 724.9 731.0 737.4 744.6
[131] 752.0 759.9 766.3 772.6 778.7 786.4 792.7 799.1 805.1 812.0 818.7 825.6 831.4
[144] 838.2 844.9 851.6 857.9 864.4 870.6 876.5

3. compose( ) function

compose() is a function allows to combine multiple functions into a single composite function. The resulting function applies each of the input functions in turn, with the output of one function being used as the input to the next function.

R
library(purrr)

my_func <- compose(sqrt, function(x) x^2)

map_dbl(mtcars$mpg, my_func)

Output:

> map_dbl(mtcars$mpg, my_func)
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4
[17] 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4

4. partial( ) function

The partial() function in the purrr package allows to create a new function by fixing one or more of the arguments of an existing function. The resulting function can then be used with the remaining unfixed arguments.

R
library(purrr)

my_cars <- data.frame(
  weight = c(1000, 2000, 3000),
  horsepower = c(100, 200, 300)
)

mpg_func <- partial(function(df, wt, hp) {
  df$mpg <- wt/hp
  df
}, wt = my_cars$weight)

map(my_cars, mpg_func, hp = 150)

Output:

> map(my_cars, mpg_func, hp = 150)
$weight
$weight[[1]]
[1] 1000

$weight[[2]]
[1] 2000

$weight[[3]]
[1] 3000

$weight$mpg
[1] 6.666667 13.333333 20.000000


$horsepower
$horsepower[[1]]
[1] 100

$horsepower[[2]]
[1] 200

$horsepower[[3]]
[1] 300

$horsepower$mpg
[1] 6.666667 13.333333 20.000000.

In this article, we explored the purrr package in R and covered key functions like map(), reduce() and accumulate(), which simplify data manipulation and improve code readability.


Next Article
Article Tags :

Similar Reads