calc_shares makes it easy to divide values by some denominator within the same long-shaped data frame. For example, it works well for a table of population groups for multiple locations where you want to divide population counts by some total population. It optionally handles dividing margins of error. Denote locations or other groupings by using a grouped data frame, passing bare column names to ..., or both.

calc_shares(
  .data,
  ...,
  group = group,
  denom = "total_pop",
  value = estimate,
  moe = NULL,
  digits = 3,
  estimate = NULL
)

Arguments

.data

A data frame

...

Optional; bare column names to be used for groupings.

group

Bare column name where groups are given--that is, the denominator value should be found in this column

denom

String; denominator to filter from group

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, MOE of shares will be included in output

digits

Number of digits to round to; defaults to 3.

estimate

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

Value

A tibble/data frame with shares (and optionally MOE of shares) of subgroup values within a denominator group. Shares given for denominator group will be blank.

Examples

edu %>%
  dplyr::group_by(name) %>%
  calc_shares(group = variable, denom = "age25plus",
              value = estimate, moe = moe)
#> # A tibble: 15 × 6
#> # Groups:   name [5]
#>    name       variable  estimate   moe  share sharemoe
#>    <chr>      <fct>        <dbl> <dbl>  <dbl>    <dbl>
#>  1 Bethany    age25plus     4013   148 NA       NA    
#>  2 Bethany    bachelors      994   174  0.248    0.042
#>  3 Bethany    masters        655   153  0.163    0.038
#>  4 East Haven age25plus    21230   490 NA       NA    
#>  5 East Haven bachelors     2998   361  0.141    0.017
#>  6 East Haven masters       1280   229  0.06     0.011
#>  7 Hamden     age25plus    40110   721 NA       NA    
#>  8 Hamden     bachelors     8587   669  0.214    0.016
#>  9 Hamden     masters       6171   568  0.154    0.014
#> 10 New Haven  age25plus    81231  1252 NA       NA    
#> 11 New Haven  bachelors    12582   820  0.155    0.01 
#> 12 New Haven  masters       8236   582  0.101    0.007
#> 13 West Haven age25plus    36031   765 NA       NA    
#> 14 West Haven bachelors     4865   533  0.135    0.015
#> 15 West Haven masters       2267   388  0.063    0.011

race_pops %>%
  calc_shares(name, group = variable, denom = "total",
              value = estimate, moe = moe)
#> # A tibble: 65 × 6
#>    name     variable estimate   moe  share sharemoe
#>    <chr>    <fct>       <dbl> <dbl>  <dbl>    <dbl>
#>  1 Bethany  total        5521    24 NA       NA    
#>  2 Bethany  white        4769   267  0.864    0.048
#>  3 Bethany  black          69    79  0.012    0.014
#>  4 Bethany  asian         313   118  0.057    0.021
#>  5 Bethany  latino        286   208  0.052    0.038
#>  6 Branford total       28084    28 NA       NA    
#>  7 Branford white       25096   435  0.894    0.015
#>  8 Branford black         349   165  0.012    0.006
#>  9 Branford asian        1211   304  0.043    0.011
#> 10 Branford latino       1033   285  0.037    0.01 
#> # … with 55 more rows