Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rossellhayes/crossmap

❌🗺 Apply Functions to All Combinations of List Elements
https://github.com/rossellhayes/crossmap

functional-programming r rstats tidyverse

Last synced: 3 months ago
JSON representation

❌🗺 Apply Functions to All Combinations of List Elements

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)

# remotes::install_github("GuangchuangYu/badger")
library(badger)
```

# crossmap

`r badge_cran_release(color = "brightgreen")`
[![crossmap status badge](https://rossellhayes.r-universe.dev/badges/crossmap)](https://rossellhayes.r-universe.dev)
`r badge_lifecycle("stable")`
`r badge_license(color = "blueviolet")`
`r badge_github_actions(action = "R-CMD-check")`
`r badge_codecov()`
`r badge_codefactor()`
`r badge_dependencies()`

**crossmap** provides an extension to [**purrr**](https://github.com/tidyverse/purrr)'s family of mapping functions.
`xmap()` works like `purrr::pmap()`, but applies a function to every combination of elements in a list of inputs.

**crossmap** also includes a few other general purpose and specialized functions for working with combinations of list elements.

## Installation

You can install the released version of **crossmap** from [CRAN](https://cran.r-project.org/package=crossmap) with:

``` {r eval = FALSE}
install.packages("crossmap")
```

or the development version from [GitHub](https://github.com/rossellhayes/crossmap) with:

```{r eval = FALSE}
# install.packages("pak")
pak::pkg_install("rossellhayes/crossmap")
```

## Usage

```{r include = FALSE}
library(dplyr)
library(purrr)
library(crossmap)
```

While `purrr::pmap()` applies a function to list elements pairwise, `xmap()` applies a function to all combinations of elements.

```{r}
pmap_chr(list(1:3, 1:3), ~ paste(.x, "*", .y, "=", .x * .y))
xmap_chr(list(1:3, 1:3), ~ paste(.x, "*", .y, "=", .x * .y))
```

`xmap_mat()` formats `xmap()` results into a matrix.

```{r}
xmap_mat(list(1:3, 1:6), prod)
```

**crossmap** also integrates with [**furrr**](https://github.com/DavisVaughan/furrr) to offer parallelized versions of the `xmap()` functions.

```{r}
future::plan("multisession")
future_xmap_chr(list(1:3, 1:3), ~ paste(.x, "*", .y, "=", .x * .y))
```

`cross_fit()` is an easy wrapper for an important use of **crossmap**, crossing model specifications with different formulas, subsets, and weights.

```{r warning = FALSE}
cross_fit(
mtcars,
formulas = list(hp = mpg ~ hp, drat = mpg ~ drat),
cols = c(cyl, vs),
weights = c(wt, NA)
)
```

`cross_list()` finds all combinations of elements from a set of lists.

```{r}
cross_list(number = 1:3, letter = letters[1:3])
cross_tbl(number = 1:3, letter = letters[1:3])
```

And `cross_join()` finds all combinations of the rows of data frames.

```{r}
cross_join(
tibble(
color = c("red", "yellow", "orange"),
fruit = c("apple", "banana", "cantaloupe")
),
tibble(dessert = c("cupcake", "muffin", "streudel"), makes = c(8, 6, 1))
)
```

`map_vec()` and variants automatically determine output types.
This means you don't have to worry about adding `_int()`, `_dbl()` or `_chr()`.

```{r}
map_vec(sample(5), ~ . ^ 2)
map_vec(c("apple", "banana", "cantaloupe"), paste0, "s")
```

---

Hex sticker font is [Source Sans by Adobe](https://github.com/adobe-fonts/source-sans).

Please note that **crossmap** is released with a [Contributor Code of Conduct](https://www.contributor-covenant.org/version/2/0/code_of_conduct/).