Movatterモバイル変換


[0]ホーム

URL:


purrrpurrr website

CRAN_Status_BadgeCodecov test coverageR-CMD-check

Overview

purrr enhances R’s functional programming (FP) toolkit by providing acomplete and consistent set of tools for working with functions andvectors. If you’ve never heard of FP before, the best place to start isthe family ofmap() functions which allow you to replacemany for loops with code that is both more succinct and easier to read.The best place to learn about themap() functions is theiteration chapter in R forData 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 thesummary, then extract the R2.

library(purrr)mtcars|>split(mtcars$cyl)|># from base Rmap(\(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 functionsover the equivalents in base R:

There are two less obvious advantages:


[8]ページ先頭

©2009-2025 Movatter.jp