Using summarise_at(). Weighted mean Tidyverse approach
Survey analysis using R. Mastering Tidyverse summarise_at() function to achieve aggregated metrics and statistics.
Supose you are analysing survey data. You are asked to get the mean in a representative way, weighting your individuals depending on the number of members of their segment.
library(tidyverse)
survey_data <- tribble(
~id, ~region1, ~region2, ~gender, ~q1, ~q2,
1,"sp","mad","m", 2,5,
2,"it", "bol", "m", 5, 10,
3,"sp", "bar", "f", 2, 2,
4,"sp", "bar", "f", 7, 7,
5,"it", "bol", "m", 2, 7)
survey_data %>%
group_by(region1, region2, gender) %>%
mutate(weight = 1/n()) %>%
ungroup() %>%
summarise_at(vars(contains("q")),
funs(weighted_mean = sum(. * weight)/sum(weight)))
q1_weighted_mean | q2_weighted_mean |
---|---|
3.333333 | 6 |