Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/rossellhayes/crossmap
- Owner: rossellhayes
- License: other
- Created: 2020-07-01T09:26:59.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-12T23:30:00.000Z (about 2 years ago)
- Last Synced: 2024-10-12T12:16:27.849Z (3 months ago)
- Topics: functional-programming, r, rstats, tidyverse
- Language: R
- Homepage: https://crossmap.rossellhayes.com
- Size: 907 KB
- Stars: 19
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
- jimsghstars - rossellhayes/crossmap - ❌🗺 Apply Functions to All Combinations of List Elements (R)
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/).