xmap: Map over each combination of list elements

View source: R/xmap.R

xmapR Documentation

Map over each combination of list elements

Description

These functions are variants of purrr::pmap() that iterate over each combination of elements in a list.

Usage

xmap(.l, .f, ...)

xmap_chr(.l, .f, ...)

xmap_dbl(.l, .f, ...)

xmap_dfc(.l, .f, ...)

xmap_dfr(.l, .f, ..., .id = NULL)

xmap_int(.l, .f, ...)

xmap_lgl(.l, .f, ...)

xmap_raw(.l, .f, ...)

xwalk(.l, .f, ...)

Arguments

.l

A list of vectors, such as a data frame. The length of .l determines the number of arguments that .f will be called with. List names will be used if present.

.f

A function, formula, or vector (not necessarily atomic).

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:

  • For a single argument function, use .

  • For a two argument function, use .x and .y

  • For more arguments, use ..1, ..2, ..3 etc

This syntax allows you to create very compact anonymous functions.

If character vector, numeric vector, or list, it is converted to an extractor function. Character vectors index by name and numeric vectors index by position; use a list to index by position and name at different levels. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to .f

.id

Either a string or NULL. If a string, the output will contain a variable with that name, storing either the name (if .x is named) or the index (if .x is unnamed) of the input. If NULL, the default, no variable will be created.

Only applies to _dfr variant.

Details

Typed variants return a vector of the specified type. To automatically determine type, try xmap_vec().

To return results as a matrix or array, try xmap_mat() and xmap_arr().

Note that a data frame is a very important special case, in which case xmap() and xwalk() apply the function .f to each row. xmap_dfr() and xmap_dfc() return data frames created by row-binding and column-binding respectively.

Value

An atomic vector, list, or data frame, depending on the suffix. Atomic vectors and lists will be named if the first element of .l is named.

If all input is length 0, the output will be length 0. If any input is length 1, it will be recycled to the length of the longest.

See Also

xmap_vec() to automatically determine output type.

xmap_mat() and xmap_arr() to return results in a matrix or array.

future_xmap() to run xmap functions with parallel processing.

cross_fit() to apply multiple models to multiple subsets of data.

cross_list() to find combinations of list elements.

purrr::map(), purrr::map2(), and purrr::pmap() for other mapping functions.

Examples

xmap(list(1:5, 1:5), ~ .y * .x)
xmap_dbl(list(1:5, 1:5), ~ .y * .x)
xmap_chr(list(1:5, 1:5), ~ paste(.y, "*", .x, "=", .y * .x))

apples_and_bananas <- list(
  x = c("apples", "bananas"),
  pattern = "a",
  replacement = c("oo", "ee")
)

xmap_chr(apples_and_bananas, gsub)

formulas <- list(mpg ~ wt, mpg ~ hp)
subsets  <- split(mtcars, mtcars$cyl)

xmap(list(subsets, formulas), ~ lm(.y, data = .x))
xmap(list(data = subsets, formula = formulas), lm)

crossmap documentation built on Jan. 13, 2023, 1:13 a.m.