• Stars
    star
    1,185
  • Rank 35,896 (Top 0.8 %)
  • Language
    R
  • License
    Other
  • Created about 9 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A functional programming toolkit for R

purrr

CRAN_Status_Badge Codecov test coverage R-CMD-check

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 in R for data science.

Installation

# 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 R2.

library(purrr)

mtcars |> 
  split(mtcars$cyl) |>  # from base R
  map(\(df) lm(mpg ~ wt, data = df)) |> 
  map(summary) %>%
  map_dbl("r.squared")
#>         4         6         8 
#> 0.5086326 0.4645102 0.4229655

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).

More Repositories

1

ggplot2

An implementation of the Grammar of Graphics in R
R
6,128
star
2

dplyr

dplyr: A grammar of data manipulation
R
4,556
star
3

tidyverse

Easily install and load packages from the tidyverse
R
1,480
star
4

rvest

Simple web scraping for R
R
1,418
star
5

tidyr

Tidy Messy Data
R
1,281
star
6

readr

Read flat files (csv, tsv, fwf) into R
R
959
star
7

magrittr

Improve the readability of R code with the pipe
R
942
star
8

datascience-box

Data Science Course in a Box
JavaScript
852
star
9

reprex

Render bits of R code for sharing, e.g., on GitHub or StackOverflow.
R
716
star
10

lubridate

Make working with dates in R just that little bit easier
R
693
star
11

readxl

Read excel files (.xls and .xlsx) into R ๐Ÿ–‡
C++
693
star
12

dtplyr

Data table backend for dplyr
R
639
star
13

multidplyr

A dplyr backend that partitions a data frame over multiple processes
R
631
star
14

tibble

A modern re-imagining of the data frame
R
623
star
15

vroom

Fast reading of delimited files
C++
589
star
16

stringr

A fresh approach to string manipulation in R
R
544
star
17

forcats

๐Ÿˆ๐Ÿˆ๐Ÿˆ๐Ÿˆ: tools for working with categorical variables (factors)
R
522
star
18

dbplyr

Database (DBI) backend for dplyr
R
416
star
19

haven

Read SPSS, Stata and SAS files from R
C
414
star
20

modelr

Helper functions for modelling
R
399
star
21

googlesheets4

Google Spreadsheets R API (reboot of the googlesheets package)
R
330
star
22

googledrive

Google Drive R API
R
297
star
23

style

The tidyverse style guide for R code
HTML
275
star
24

tidyverse.org

Source of tidyverse.org
HTML
172
star
25

design

Tidyverse design principles
R
157
star
26

hms

A simple class for storing time-of-day values
R
134
star
27

nycflights13

An R data package containing all out-bound flights from NYC in 2013 + useful metdata
R
116
star
28

tidyversedashboard

Tidyverse activity dashboard
R
69
star
29

tidy-dev-day

Tidyverse developer day
57
star
30

tidyeval

A guide to tidy evaluation
CSS
51
star
31

dsbox

Companion R package to Data Science Course in a Box
R
44
star
32

blob

A simple S3 class for representing BLOBs
R
42
star
33

tidytemplate

A pkgdown template for core tidyverse packages
SCSS
42
star
34

funs

Collection of low-level functions for working with vctrs
R
29
star
35

website-analytics

Web analytics for tidyverse + r-lib sites
R
28
star
36

code-review

23
star
37

tidyups

19
star
38

ggplot2-docs

ggplot2 documentation. Auto-generated from ggplot2 sources by pkgdown
HTML
9
star