Purrr is a popular R package that provides a set of tools for working with functions and vectors in a more efficient and intuitive way. It is designed to simplify common data manipulation tasks, such as filtering, mapping, and summarizing data, by providing a consistent and easy-to-use syntax.
One of the key features of Purrr is its ability to work with functions as first-class objects. This means that functions can be passed as arguments to other functions, returned as values, and stored in data structures. This makes it easy to create complex data transformations by chaining together simple functions.
Another important feature of Purrr is its support for vectorized operations. This means that many operations can be applied to entire vectors of data at once, rather than having to loop over each element individually. This can greatly improve the speed and efficiency of data manipulation tasks.
Functions
Function | Description |
---|---|
map() | Apply a function to each element of a vector or list |
map2() | Apply a function to two vectors or lists element-wise |
map_chr() | Apply a function to each element of a vector or list and return a character vector |
map_dbl() | Apply a function to each element of a vector or list and return a double vector |
map_int() | Apply a function to each element of a vector or list and return an integer vector |
map_lgl() | Apply a function to each element of a vector or list and return a logical vector |
map_if() | Apply a function to each element of a vector or list if it meets a certain condition |
map_at() | Apply a function to specific elements of a vector or list |
map_depth() | Apply a function to each element of a nested list |
pmap() | Apply a function to multiple vectors or lists element-wise |
pmap_chr() | Apply a function to multiple vectors or lists element-wise and return a character vector |
pmap_dbl() | Apply a function to multiple vectors or lists element-wise and return a double vector |
pmap_int() | Apply a function to multiple vectors or lists element-wise and return an integer vector |
pmap_lgl() | Apply a function to multiple vectors or lists element-wise and return a logical vector |
reduce() | Reduce a vector or list to a single value using a function |
accumulate() | Accumulate a vector or list using a function |
Arguments
Argument | Description |
---|---|
.f | The function to apply |
.x | The vector or list to apply the function to |
.y | The second vector or list to apply the function to |
.keep | Whether to keep the original structure of the input |
.id | The name of the list element to use as an identifier |
.names | The names to use for the output list |
.default | The default value to use if an element is missing |
.f.args | Additional arguments to pass to the function |
.f.call | Whether to include the function call in the output |
.progress | Whether to display a progress bar |
.env | The environment to evaluate the function in |
Examples
Map
library(purrr)
# Apply a function to each element of a vector
map(1:5, function(x) x^2)
# Apply a function to each element of a list
map(list(1:5, 6:10), sum)
# Apply a function to each element of a vector if it meets a certain condition
map_if(1:5, is.even, function(x) x^2)
# Apply a function to specific elements of a vector or list
map_at(list(1:5, 6:10), 1, function(x) x^2)
Reduce
# Reduce a vector to a single value
reduce(1:5, function(x, y) x + y)
# Reduce a list to a single value
reduce(list(1:5, 6:10), function(x, y) sum(x) + sum(y))
Pmap
# Apply a function to multiple vectors element-wise
pmap(list(1:5, 6:10), function(x, y) x + y)
# Apply a function to multiple lists element-wise
pmap(list(list(1, 2, 3), list(4, 5, 6)), sum)