View source: R/grouped_weighted_percentage.R
grouped_weighted_percentage | R Documentation |
This definitely requires explanation, but an example should make it clear. The motivation of this function is to compute the percentage of people within a municipality that have a certain demographic characteristic. The catch is that the data are not reported by municipality, but by census tract, and census tract boundaries don't match the boundaries of the municipalities. So, to compute the percentage of people who have health insurance in Racine, WI, we need to count the number of people in each census tract that overlaps with Racine, weight the tracts by how much of their populations are actually within the city's borders, and then find the weighted percent with insurance by dividing the weighted sum of people with insurance by the weighted sum of all people.
grouped_weighted_percentage(
.x,
groups,
in_value_field,
all_value_field,
in_weight_field,
all_weight_field
)
.x |
a tibble of data |
groups |
a vector of strings that name grouping variables |
in_value_field |
the field with counts of in-group members |
all_value_field |
the field with counts of all items |
in_weight_field |
the field with weights for in-group members |
all_weight_field |
the field with weights for all items |
a tibble with weighted percents per group
toy_data <- tibble::tribble(
~ City, ~ Tract, ~ Insured, ~ All, ~ `City Pop`, ~ `Tract Pop`,
"Foo", "A", 10L, 10L, 0L, 99L,
"Bar", "A", 10L, 10L, 22L, 99L,
"Foo", "B", 25L, 42L, 1200L, 1900L,
"Bar", "B", 25L, 42L, 700L, 1900L
)
grouped_weighted_percentage(toy_data,
groups = "City",
in_value_field = "Insured",
all_value_field = "All",
in_weight_field = "City Pop",
all_weight_field = "Tract Pop")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.