purrr v0.2.5
Monthly downloads
Functional Programming Tools
A complete and consistent functional programming toolkit for R.
Readme
purrr 
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("devtools")
devtools::install_github("tidyverse/purrr")
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(.$cyl) %>% # from base R
map(~ lm(mpg ~ wt, data = .)) %>%
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 errror.All
map()
functions either accept function, formulas (used for succinctly generating anonymous functions), a character vector (used to extract components by name), or a numeric vector (used to extract by position).
Functions in purrr
Name | Description | |
keep | Keep or discard elements using a predicate function. | |
map | Apply a function to each element of a vector | |
map2 | Map over multiple inputs simultaneously. | |
array-coercion | Coerce array to list | |
as_mapper | Convert an object into a mapper function | |
null-default | Default value for NULL. | |
every | Do every or some elements of a list satisfy a predicate? | |
partial | Partial apply a function, filling in some arguments. | |
detect | Find the value or position of the first match. | |
cross | Produce all combinations of list elements | |
set_names | Set names in a vector | |
get-attr | Infix attribute accessor | |
splice | Splice objects and lists of objects into a list | |
flatten | Flatten a list of lists into a simple vector. | |
invoke | Invoke functions. | |
has_element | Does a list contain an object? | |
transpose | Transpose a list. | |
when | Match/validate a set of conditions for an object and continue with the action associated with the first valid match. | |
is_numeric | Test is an object is integer or double | |
as_vector | Coerce a list to a vector | |
vec_depth | Compute the depth of a vector | |
accumulate | Accumulate recursive folds across a list | |
%>% | Pipe operator | |
compose | Compose multiple functions | |
along | Helper to create vectors with matching length. | |
head_while | Find head/tail that all satisfies a predicate. | |
lmap | Apply a function to list-elements of a list | |
list_modify | Modify a list | |
imap | Apply a function to each element of a vector, and its index | |
rerun | Re-run expressions multiple times. | |
pluck | Pluck out a single an element from a vector or environment | |
modify | Modify elements selectively | |
safely | Capture side effects. | |
prepend | Prepend a vector | |
rdunif | Generate random sample from a discrete uniform distribution | |
rbernoulli | Generate random sample from a Bernoulli distribution | |
purrr-package | purrr: Functional Programming Tools | |
negate | Negate a predicate function. | |
reduce | Reduce a list to a single value by iteratively applying a binary function. | |
reexports | Objects exported from other packages | |
lift | Lift the domain of a function | |
No Results! |
Vignettes of purrr
Name | ||
other-langs.Rmd | ||
No Results! |
Last month downloads
Details
License | GPL-3 | file LICENSE |
URL | http://purrr.tidyverse.org, https://github.com/tidyverse/purrr |
BugReports | https://github.com/tidyverse/purrr/issues |
VignetteBuilder | knitr |
Encoding | UTF-8 |
LazyData | true |
RoxygenNote | 6.0.1 |
NeedsCompilation | yes |
Packaged | 2018-05-29 14:46:54 UTC; lionel |
Repository | CRAN |
Date/Publication | 2018-05-29 16:46:33 UTC |
suggests | covr , dplyr (>= 0.7.5) , knitr , rmarkdown , testthat |
imports | magrittr (>= 1.5) , rlang (>= 0.1) , tibble |
depends | R (>= 3.1) |
Contributors | RStudio, Hadley Wickham |
Include our badge in your README
[](http://www.rdocumentation.org/packages/purrr)