EVOLUTION-MANAGER
Edit File: map_if.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 conditionally</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 map_if {purrr}"><tr><td>map_if {purrr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Apply a function to each element of a vector conditionally</h2> <h3>Description</h3> <p>The functions <code>map_if()</code> and <code>map_at()</code> take <code>.x</code> as input, apply the function <code>.f</code> to some of the elements of <code>.x</code>, and return a list of the same length as the input. </p> <ul> <li> <p><code>map_if()</code> takes a predicate function <code>.p</code> as input to determine which elements of <code>.x</code> are transformed with <code>.f</code>. </p> </li> <li> <p><code>map_at()</code> takes a vector of names or positions <code>.at</code> to specify which elements of <code>.x</code> are transformed with <code>.f</code>. </p> </li></ul> <ul> <li> <p><code>map_depth()</code> allows to apply <code>.f</code> to a specific depth level of a nested vector. </p> </li></ul> <h3>Usage</h3> <pre> map_if(.x, .p, .f, ..., .else = NULL) map_at(.x, .at, .f, ...) map_depth(.x, .depth, .f, ..., .ragged = FALSE) </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>.p</code></td> <td> <p>A single predicate function, a formula describing such a predicate function, or a logical vector of the same length as <code>.x</code>. Alternatively, if the elements of <code>.x</code> are themselves lists of objects, a string indicating the name of a logical element in the inner lists. Only those elements where <code>.p</code> evaluates to <code>TRUE</code> will be modified.</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>.else</code></td> <td> <p>A function applied to elements of <code>.x</code> for which <code>.p</code> returns <code>FALSE</code>.</p> </td></tr> <tr valign="top"><td><code>.at</code></td> <td> <p>A character vector of names, positive numeric vector of positions to include, or a negative numeric vector of positions to exlude. Only those elements corresponding to <code>.at</code> will be modified. If the <code>tidyselect</code> package is installed, you can use <code>vars()</code> and the <code>tidyselect</code> helpers to select elements.</p> </td></tr> <tr valign="top"><td><code>.depth</code></td> <td> <p>Level of <code>.x</code> to map on. Use a negative value to count up from the lowest level of the list. </p> <ul> <li> <p><code>map_depth(x, 0, fun)</code> is equivalent to <code>fun(x)</code>. </p> </li> <li> <p><code>map_depth(x, 1, fun)</code> is equivalent to <code>x <- map(x, fun)</code> </p> </li> <li> <p><code>map_depth(x, 2, fun)</code> is equivalent to <code>x <- map(x, ~ map(., fun))</code> </p> </li></ul> </td></tr> <tr valign="top"><td><code>.ragged</code></td> <td> <p>If <code>TRUE</code>, will apply to leaves, even if they're not at depth <code>.depth</code>. If <code>FALSE</code>, will throw an error if there are no elements at depth <code>.depth</code>.</p> </td></tr> </table> <h3>See Also</h3> <p>Other map variants: <code><a href="imap.html">imap</a>()</code>, <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.html">map</a>()</code>, <code><a href="modify.html">modify</a>()</code> </p> <h3>Examples</h3> <pre> # Use a predicate function to decide whether to map a function: map_if(iris, is.factor, as.character) # Specify an alternative with the `.else` argument: map_if(iris, is.factor, as.character, .else = as.integer) # Use numeric vector of positions select elements to change: iris %>% map_at(c(4, 5), is.numeric) # Use vector of names to specify which elements to change: iris %>% map_at("Species", toupper) # Use `map_depth()` to recursively traverse nested vectors and map # a function at a certain depth: x <- list(a = list(foo = 1:2, bar = 3:4), b = list(baz = 5:6)) str(x) map_depth(x, 2, paste, collapse = "/") # Equivalent to: map(x, map, paste, collapse = "/") </pre> <hr /><div style="text-align: center;">[Package <em>purrr</em> version 0.3.4 <a href="00Index.html">Index</a>]</div> </body></html>