EVOLUTION-MANAGER
Edit File: imap.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: Apply a function to each element of a vector, and its index</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 imap {purrr}"><tr><td>imap {purrr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Apply a function to each element of a vector, and its index</h2> <h3>Description</h3> <p><code>imap_xxx(x, ...)</code>, an indexed map, is short hand for <code>map2(x, names(x), ...)</code> if <code>x</code> has names, or <code>map2(x, seq_along(x), ...)</code> if it does not. This is useful if you need to compute on both the value and the position of an element. </p> <h3>Usage</h3> <pre> imap(.x, .f, ...) imap_lgl(.x, .f, ...) imap_chr(.x, .f, ...) imap_int(.x, .f, ...) imap_dbl(.x, .f, ...) imap_raw(.x, .f, ...) imap_dfr(.x, .f, ..., .id = NULL) imap_dfc(.x, .f, ...) iwalk(.x, .f, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>.x</code></td> <td> <p>A list or atomic vector.</p> </td></tr> <tr valign="top"><td><code>.f</code></td> <td> <p>A function, formula, or vector (not necessarily atomic). </p> <p>If a <strong>function</strong>, it is used as is. </p> <p>If a <strong>formula</strong>, e.g. <code>~ .x + 2</code>, it is converted to a function. There are three ways to refer to the arguments: </p> <ul> <li><p> For a single argument function, use <code>.</code> </p> </li> <li><p> For a two argument function, use <code>.x</code> and <code>.y</code> </p> </li> <li><p> For more arguments, use <code>..1</code>, <code>..2</code>, <code>..3</code> etc </p> </li></ul> <p>This syntax allows you to create very compact anonymous functions. </p> <p>If <strong>character vector</strong>, <strong>numeric vector</strong>, or <strong>list</strong>, 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 <code>.default</code> will be returned.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Additional arguments passed on to the mapped function.</p> </td></tr> <tr valign="top"><td><code>.id</code></td> <td> <p>Either a string or <code>NULL</code>. If a string, the output will contain a variable with that name, storing either the name (if <code>.x</code> is named) or the index (if <code>.x</code> is unnamed) of the input. If <code>NULL</code>, the default, no variable will be created. </p> <p>Only applies to <code style="white-space: pre;">_dfr</code> variant.</p> </td></tr> </table> <h3>Value</h3> <p>A vector the same length as <code>.x</code>. </p> <h3>See Also</h3> <p>Other map variants: <code><a href="invoke.html">invoke</a>()</code>, <code><a href="lmap.html">lmap</a>()</code>, <code><a href="map2.html">map2</a>()</code>, <code><a href="map_if.html">map_if</a>()</code>, <code><a href="map.html">map</a>()</code>, <code><a href="modify.html">modify</a>()</code> </p> <h3>Examples</h3> <pre> # Note that when using the formula shortcut, the first argument # is the value, and the second is the position imap_chr(sample(10), ~ paste0(.y, ": ", .x)) iwalk(mtcars, ~ cat(.y, ": ", median(.x), "\n", sep = "")) </pre> <hr /><div style="text-align: center;">[Package <em>purrr</em> version 0.3.4 <a href="00Index.html">Index</a>]</div> </body></html>