Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tidyverse/purrr
A functional programming toolkit for R
https://github.com/tidyverse/purrr
functional-programming r
Last synced: 3 days ago
JSON representation
A functional programming toolkit for R
- Host: GitHub
- URL: https://github.com/tidyverse/purrr
- Owner: tidyverse
- License: other
- Created: 2014-11-29T17:33:40.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2024-08-21T21:15:45.000Z (4 months ago)
- Last Synced: 2024-12-02T19:07:53.388Z (10 days ago)
- Topics: functional-programming, r
- Language: R
- Homepage: https://purrr.tidyverse.org/
- Size: 9.42 MB
- Stars: 1,277
- Watchers: 63
- Forks: 272
- Open Issues: 22
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Support: .github/SUPPORT.md
Awesome Lists containing this project
- awesome-R - purrr - A functional programming toolkit for R ![purrr](https://cranlogs.r-pkg.org/badges/purrr) (2017)
- awesome-starred - purrr - A functional programming toolkit for R (R)
- jimsghstars - tidyverse/purrr - A functional programming toolkit for R (R)
README
---
output: github_document
---```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```# purrr
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/purrr)](https://cran.r-project.org/package=purrr)
[![Codecov test coverage](https://codecov.io/gh/tidyverse/purrr/branch/master/graph/badge.svg)](https://app.codecov.io/gh/tidyverse/purrr?branch=master)
[![R-CMD-check](https://github.com/tidyverse/purrr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidyverse/purrr/actions/workflows/R-CMD-check.yaml)## Overview
purrr enhances R's functional programming (FP) toolkit by providing a complete and consistent set of tools for working with functions and vectors. If you've never heard of FP before, the best place to start is the family of `map()` functions which allow you to replace many for loops with code that is both more succinct and easier to read. The best place to learn about the `map()` functions is the [iteration chapter](https://r4ds.hadley.nz/iteration) in R for Data Science.
## Installation
```{r, eval = FALSE}
# The easiest way to get purrr is to install the whole tidyverse:
install.packages("tidyverse")# Alternatively, install just purrr:
install.packages("purrr")# Or the the development version from GitHub:
# install.packages("pak")
pak::pak("tidyverse/purrr")
```## Cheatsheet
## Usage
The following example uses purrr to solve a fairly realistic problem: split a data frame into pieces, fit a model to each piece, compute the summary, then extract the R^2^.
```{r, eval = getRversion() >= "4.1"}
library(purrr)mtcars |>
split(mtcars$cyl) |> # from base R
map(\(df) lm(mpg ~ wt, data = df)) |>
map(summary) |>
map_dbl("r.squared")
```This example illustrates some of the advantages of purrr functions over the equivalents in base R:
* The first argument is always the data, so purrr works naturally with the pipe.
* All purrr functions are type-stable. They always return the advertised output
type (`map()` returns lists; `map_dbl()` returns double vectors), or they
throw an error.
* All `map()` functions accept functions (named, anonymous, and lambda),
character vector (used to extract components by name), or numeric vectors
(used to extract by position).