purrr
所属分类:hotest
开发工具:R
文件大小:0KB
下载次数:0
上传日期:2023-04-29 07:02:49
上 传 者:
sh-1993
说明: R的函数式编程工具包
(A functional programming toolkit for R)
文件列表:
.Rbuildignore (433, 2023-11-01)
DESCRIPTION (1077, 2023-11-01)
LICENSE (43, 2023-11-01)
LICENSE.md (1072, 2023-11-01)
NAMESPACE (3923, 2023-11-01)
NEWS.md (37019, 2023-11-01)
R/ (0, 2023-11-01)
R/adverb-auto-browse.R (1571, 2023-11-01)
R/adverb-compose.R (2425, 2023-11-01)
R/adverb-insistently.R (2322, 2023-11-01)
R/adverb-negate.R (522, 2023-11-01)
R/adverb-partial.R (7247, 2023-11-01)
R/adverb-possibly.R (847, 2023-11-01)
R/adverb-quietly.R (1290, 2023-11-01)
R/adverb-safely.R (2036, 2023-11-01)
R/adverb-slowly.R (853, 2023-11-01)
R/arrays.R (2664, 2023-11-01)
R/cleancall.R (104, 2023-11-01)
R/coerce.R (1119, 2023-11-01)
R/compat-obj-type.R (8098, 2023-11-01)
R/compat-types-check.R (10473, 2023-11-01)
R/conditions.R (3262, 2023-11-01)
R/deprec-along.R (712, 2023-11-01)
R/deprec-cross.R (6834, 2023-11-01)
... ...
# 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.had.co.nz/iteration.html) in R for data science.
## Installation
``` r
# 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
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).
近期下载者:
相关文件:
收藏者: