-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
122 lines (91 loc) · 3.39 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{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 <img src="man/figures/logo.png?raw=TRUE" align="right" height="138" />
<!-- badges: start -->
`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()`
<!-- badges: end -->
**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/).