Site icon R-bloggers

Using summarise_at(). Weighted mean Tidyverse approach

[This article was first published on R | TypeThePipe, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

To leave a comment for the author, please follow the link and comment on their blog: R | TypeThePipe.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.