Collapse variable into groups and sum

add_grps(
  .data,
  grp_list,
  group = group,
  value = estimate,
  moe = NULL,
  estimate = NULL
)

Arguments

.data

A data frame; will honor grouping

grp_list

A named list of groups to collapse group into

group

Bare column name giving groups in data; will be converted to factor

value

Bare column name of values. Replaces estimate argument, but (for now) still defaults to a column named estimate

moe

Bare column name of margins of error; if supplied, MOEs of sums will be included in output

estimate

Soft deprecated; use value. Bare column name of estimates or values

Value

A data frame/tibble with sums of estimate. Retains grouping columns

Examples

edu_list <- list(ages25plus = 1, less_than_high_school = 2:16,
    high_school = 17:18, some_college_or_aa = 19:21, bachelors_plus = 22:25)

edu_detail %>%
  dplyr::group_by(name) %>%
  add_grps(edu_list, group = variable, value = estimate, moe = moe)
#> # A tibble: 25 × 4
#> # Groups:   name [5]
#>    name       variable              estimate   moe
#>    <chr>      <fct>                    <dbl> <dbl>
#>  1 Bethany    ages25plus                4013   148
#>  2 Bethany    less_than_high_school      120    83
#>  3 Bethany    high_school                866   177
#>  4 Bethany    some_college_or_aa        1053   208
#>  5 Bethany    bachelors_plus            1974   257
#>  6 East Haven ages25plus               21230   490
#>  7 East Haven less_than_high_school     2380   363
#>  8 East Haven high_school               8612   554
#>  9 East Haven some_college_or_aa        5334   511
#> 10 East Haven bachelors_plus            4904   468
#> # … with 15 more rows

edu_detail %>%
  dplyr::group_by(name) %>%
  add_grps(list(total = "ages25plus",
      aa_or_bach = c("Associate's degree", "Bachelor's degree"),
      bachelors_plus = c("Bachelor's degree", "Master's degree",
                         "Professional school degree", "Doctorate degree")),
    value = estimate, group = variable, moe = moe)
#> # A tibble: 15 × 4
#> # Groups:   name [5]
#>    name       variable       estimate   moe
#>    <chr>      <fct>             <dbl> <dbl>
#>  1 Bethany    total              4013   148
#>  2 Bethany    aa_or_bach         1306   205
#>  3 Bethany    bachelors_plus     1974   257
#>  4 East Haven total             21230   490
#>  5 East Haven aa_or_bach         4644   449
#>  6 East Haven bachelors_plus     4904   468
#>  7 Hamden     total             40110   721
#>  8 Hamden     aa_or_bach        11276   757
#>  9 Hamden     bachelors_plus    18274   967
#> 10 New Haven  total             81231  1252
#> 11 New Haven  aa_or_bach        16105   919
#> 12 New Haven  bachelors_plus    27810  1153
#> 13 West Haven total             36031   765
#> 14 West Haven aa_or_bach         7286   636
#> 15 West Haven bachelors_plus     7879   703