EVOLUTION-MANAGER
Edit File: cross.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Produce all combinations of list elements</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for cross {purrr}"><tr><td>cross {purrr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Produce all combinations of list elements</h2> <h3>Description</h3> <p><code>cross2()</code> returns the product set of the elements of <code>.x</code> and <code>.y</code>. <code>cross3()</code> takes an additional <code>.z</code> argument. <code>cross()</code> takes a list <code>.l</code> and returns the cartesian product of all its elements in a list, with one combination by element. <code>cross_df()</code> is like <code>cross()</code> but returns a data frame, with one combination by row. </p> <h3>Usage</h3> <pre> cross(.l, .filter = NULL) cross2(.x, .y, .filter = NULL) cross3(.x, .y, .z, .filter = NULL) cross_df(.l, .filter = NULL) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>.l</code></td> <td> <p>A list of lists or atomic vectors. Alternatively, a data frame. <code>cross_df()</code> requires all elements to be named.</p> </td></tr> <tr valign="top"><td><code>.filter</code></td> <td> <p>A predicate function that takes the same number of arguments as the number of variables to be combined.</p> </td></tr> <tr valign="top"><td><code>.x, .y, .z</code></td> <td> <p>Lists or atomic vectors.</p> </td></tr> </table> <h3>Details</h3> <p><code>cross()</code>, <code>cross2()</code> and <code>cross3()</code> return the cartesian product is returned in wide format. This makes it more amenable to mapping operations. <code>cross_df()</code> returns the output in long format just as <code>expand.grid()</code> does. This is adapted to rowwise operations. </p> <p>When the number of combinations is large and the individual elements are heavy memory-wise, it is often useful to filter unwanted combinations on the fly with <code>.filter</code>. It must be a predicate function that takes the same number of arguments as the number of crossed objects (2 for <code>cross2()</code>, 3 for <code>cross3()</code>, <code>length(.l)</code> for <code>cross()</code>) and returns <code>TRUE</code> or <code>FALSE</code>. The combinations where the predicate function returns <code>TRUE</code> will be removed from the result. </p> <h3>Value</h3> <p><code>cross2()</code>, <code>cross3()</code> and <code>cross()</code> always return a list. <code>cross_df()</code> always returns a data frame. <code>cross()</code> returns a list where each element is one combination so that the list can be directly mapped over. <code>cross_df()</code> returns a data frame where each row is one combination. </p> <h3>See Also</h3> <p><code><a href="../../base/html/expand.grid.html">expand.grid()</a></code> </p> <h3>Examples</h3> <pre> # We build all combinations of names, greetings and separators from our # list of data and pass each one to paste() data <- list( id = c("John", "Jane"), greeting = c("Hello.", "Bonjour."), sep = c("! ", "... ") ) data %>% cross() %>% map(lift(paste)) # cross() returns the combinations in long format: many elements, # each representing one combination. With cross_df() we'll get a # data frame in long format: crossing three objects produces a data # frame of three columns with each row being a particular # combination. This is the same format that expand.grid() returns. args <- data %>% cross_df() # In case you need a list in long format (and not a data frame) # just run as.list() after cross_df() args %>% as.list() # This format is often less pratical for functional programming # because applying a function to the combinations requires a loop out <- vector("list", length = nrow(args)) for (i in seq_along(out)) out[[i]] <- map(args, i) %>% invoke(paste, .) out # It's easier to transpose and then use invoke_map() args %>% transpose() %>% map_chr(~ invoke(paste, .)) # Unwanted combinations can be filtered out with a predicate function filter <- function(x, y) x >= y cross2(1:5, 1:5, .filter = filter) %>% str() # To give names to the components of the combinations, we map # setNames() on the product: seq_len(3) %>% cross2(., ., .filter = `==`) %>% map(setNames, c("x", "y")) # Alternatively we can encapsulate the arguments in a named list # before crossing to get named components: seq_len(3) %>% list(x = ., y = .) %>% cross(.filter = `==`) </pre> <hr /><div style="text-align: center;">[Package <em>purrr</em> version 0.3.4 <a href="00Index.html">Index</a>]</div> </body></html>