group_map {dplyr} | R Documentation

## Apply a function to each group

### Description

`group_map()` and `group_walk()` are purrr-style functions that can be used to iterate on grouped tibbles.

### Usage

    group_map(.tbl, .f, ...)

    group_walk(.tbl, .f, ...)

### Arguments

`.tbl` | A grouped tibble
`.f` | A function or formula to apply to each group. It must return a data frame. If a function, it is used as is. It should have at least 2 formal arguments. If a formula, e.g. `~ head(.x)`, it is converted to a function. In the formula, you can use `.` or `.x` to refer to the subset of rows of `.tbl` for the given group `.y` to refer to the key, a one row tibble with one column per grouping variable that identifies the group
`...` | Additional arguments passed on to `.f`

### Details

- Use `group_map()` when `summarize()` is too limited, in terms of what you need to do and return for each group. `group_map()` is good for "data frame in, data frame out". If that is too limited, you need to use a nested or split workflow.

- `group_map()` and `group_walk()` are an evolution of `do()`, if you have used that before.

Each conceptual group of the data frame is exposed to the function `.f` with two pieces of information:

- The subset of the data for the group, exposed as `.x`.

- The key, a tibble with exactly one row and columns for each grouping variable, exposed as `.y`.

`.f` must return a data frame that does not contain any of the grouping variables of `.tbl`.

For completeness, `group_map()` and `group_walk()` also work on ungrouped data frames, in that case the function is applied to the entire data frame (exposed as `.x`), and `.y` is a one row tibble with no column, consistently with `group_keys()`.

### Value

- `group_map()` row binds the data frames returned by `.f`

- `group_walk()` calls `.f` for side effects and returns the input `.tbl`, invisibly

### See Also

Other grouping functions: `group_by_all`, `group_by`, `group_indices`, `group_keys`, `group_nest`, `group_rows`, `group_size`, `group_trim`, `groups`

### Examples

    mtcars %>%
      group_by(cyl) %>%
      group_map(~ head(.x, 2L))

    if (requireNamespace("broom", quietly = TRUE)) {
      iris %>%
        group_by(Species) %>%
        group_map(~ broom::tidy(lm(Petal.Length ~ Sepal.Length, data = .x)))
    }

    iris %>%
      group_by(Species) %>%
      group_map(~ {
         quantile(.x$Petal.Length, probs = c(0.25, 0.5, 0.75)) %>%
         tibble::enframe(name = "prob", value = "quantile")
      })

    iris %>%
      group_by(Species) %>%
      group_map(~ {
        .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_map() and ungrouped data frames
    mtcars %>%
      group_map(~ head(.x, 2L))

[Package dplyr version 0.8.0.1 Index]