EVOLUTION-MANAGER
Edit File: group_map.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 group</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 group_map {dplyr}"><tr><td>group_map {dplyr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Apply a function to each group</h2> <h3>Description</h3> <a href='https://www.tidyverse.org/lifecycle/#experimental'><img src='figures/lifecycle-experimental.svg' alt='Experimental lifecycle'></a> <p><code>group_map()</code>, <code>group_modify()</code> and <code>group_walk()</code> are purrr-style functions that can be used to iterate on grouped tibbles. </p> <h3>Usage</h3> <pre> group_map(.data, .f, ..., .keep = FALSE) group_modify(.data, .f, ..., .keep = FALSE) group_walk(.data, .f, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>.data</code></td> <td> <p>A grouped tibble</p> </td></tr> <tr valign="top"><td><code>.f</code></td> <td> <p>A function or formula to apply to each group. It must return a data frame. </p> <p>If a <strong>function</strong>, it is used as is. It should have at least 2 formal arguments. </p> <p>If a <strong>formula</strong>, e.g. <code>~ head(.x)</code>, it is converted to a function. </p> <p>In the formula, you can use </p> <ul> <li> <p><code>.</code> or <code>.x</code> to refer to the subset of rows of <code>.tbl</code> for the given group </p> </li> <li> <p><code>.y</code> to refer to the key, a one row tibble with one column per grouping variable that identifies the group </p> </li></ul> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Additional arguments passed on to <code>.f</code></p> </td></tr> <tr valign="top"><td><code>.keep</code></td> <td> <p>are the grouping variables kept in <code>.x</code></p> </td></tr> </table> <h3>Details</h3> <p>Use <code>group_modify()</code> when <code>summarize()</code> is too limited, in terms of what you need to do and return for each group. <code>group_modify()</code> is good for "data frame in, data frame out". If that is too limited, you need to use a <a href="group_nest.html">nested</a> or <a href="group_split.html">split</a> workflow. <code>group_modify()</code> is an evolution of <code><a href="do.html">do()</a></code>, if you have used that before. </p> <p>Each conceptual group of the data frame is exposed to the function <code>.f</code> with two pieces of information: </p> <ul> <li><p> The subset of the data for the group, exposed as <code>.x</code>. </p> </li> <li><p> The key, a tibble with exactly one row and columns for each grouping variable, exposed as <code>.y</code>. </p> </li></ul> <p>For completeness, <code>group_modify()</code>, <code>group_map</code> and <code>group_walk()</code> also work on ungrouped data frames, in that case the function is applied to the entire data frame (exposed as <code>.x</code>), and <code>.y</code> is a one row tibble with no column, consistently with <code><a href="group_data.html">group_keys()</a></code>. </p> <h3>Value</h3> <ul> <li> <p><code>group_modify()</code> returns a grouped tibble. In that case <code>.f</code> must return a data frame. </p> </li> <li> <p><code>group_map()</code> returns a list of results from calling <code>.f</code> on each group. </p> </li> <li> <p><code>group_walk()</code> calls <code>.f</code> for side effects and returns the input <code>.tbl</code>, invisibly. </p> </li></ul> <h3>See Also</h3> <p>Other grouping functions: <code><a href="group_by.html">group_by</a>()</code>, <code><a href="group_nest.html">group_nest</a>()</code>, <code><a href="group_split.html">group_split</a>()</code>, <code><a href="group_trim.html">group_trim</a>()</code> </p> <h3>Examples</h3> <pre> # return a list mtcars %>% group_by(cyl) %>% group_map(~ head(.x, 2L)) # return a tibble grouped by `cyl` with 2 rows per group # the grouping data is recalculated mtcars %>% group_by(cyl) %>% group_modify(~ head(.x, 2L)) if (requireNamespace("broom", quietly = TRUE)) { # a list of tibbles iris %>% group_by(Species) %>% group_map(~ broom::tidy(lm(Petal.Length ~ Sepal.Length, data = .x))) # a restructured grouped tibble iris %>% group_by(Species) %>% group_modify(~ broom::tidy(lm(Petal.Length ~ Sepal.Length, data = .x))) } # a list of vectors iris %>% group_by(Species) %>% group_map(~ quantile(.x$Petal.Length, probs = c(0.25, 0.5, 0.75))) # to use group_modify() the lambda must return a data frame iris %>% group_by(Species) %>% group_modify(~ { quantile(.x$Petal.Length, probs = c(0.25, 0.5, 0.75)) %>% tibble::enframe(name = "prob", value = "quantile") }) iris %>% group_by(Species) %>% group_modify(~ { .x %>% purrr::map_dfc(fivenum) %>% mutate(nms = c("min", "Q1", "median", "Q3", "max")) }) # group_walk() is for side effects dir.create(temp <- tempfile()) iris %>% group_by(Species) %>% group_walk(~ write.csv(.x, file = file.path(temp, paste0(.y$Species, ".csv")))) list.files(temp, pattern = "csv$") unlink(temp, recursive = TRUE) # group_modify() and ungrouped data frames mtcars %>% group_modify(~ head(.x, 2L)) </pre> <hr /><div style="text-align: center;">[Package <em>dplyr</em> version 1.0.2 <a href="00Index.html">Index</a>]</div> </body></html>