group_map_dfr: Apply a function to each group

View source: R/group_map.R View source: R/group_map.R

group_map_dfrR Documentation

Apply a function to each group

Description

group_map(), group_walk and group_map_dfr are purrr-style functions that can be used to iterate on grouped survey objects (note that group_map_dfr replaces dplyr::group_modify because we are changing the data from a tbl_svy to a regular tibble).

Usage

group_map_dfr(.data, .f, ..., .keep = FALSE)

## S3 method for class 'tbl_svy'
group_map(.data, .f, ..., .keep = FALSE)

group_map_dfr(.data, .f, ..., .keep = FALSE)

Arguments

.data

A tbl_svy object

.f

A function or purrr-style formula to apply to each group

...

Other arguments passed to .f

.keep

Whether the grouping variables are kept when passed into .f

Value

For group_map a list, for group_map_dfr a 'tbl_df', and for group_walk invisibly the original tbl_svy.

Examples

data(api, package = "survey")
dstrata <- apistrat %>%
  as_survey_design(strata = stype, weights = pw)

results <- dstrata %>%
  group_by(both) %>%
  group_map(~survey::svyglm(api00~api99 + stype, .))

# group_map_dfr calls `bind_rows` on the list returned and includes
# grouping variables. This is most useful with a package like `broom`
# but could also be used with survey package functions.
result_coef <- dstrata %>%
  group_by(both) %>%
  group_map_dfr(
    ~data.frame(
      api99_coef = coef(survey::svyglm(api00~api99 + stype, .))[["api99"]]
    )
  )


srvyr documentation built on March 7, 2023, 6:39 p.m.