library(survey)
data(api)
dstrata <- apistrat %>%
as_survey_design(strata = stype, weights = pw)
# Calculates the means by stype and also for the whole
# sample
dstrata %>%
group_by(stype) %>%
cascade(api99_mn = survey_mean(api99),
api00_mn = survey_mean(api00),
api_diff = survey_mean(api00 - api99))
# Calculates the proportions by the interaction of stype & awards
# as well as by each of those variable's groups alone, and finally
# the total as well
dstrata %>%
group_by(interact(stype, awards)) %>%
cascade(prop = survey_mean())
# Can also specify the .groupings manually, though this interface
# is a little ugly, as it requires passing a list of quosures or
# symbols you've created, rather than the usual syntax
dstrata %>%
cascade(
prop = survey_mean(),
.groupings = list(rlang::quos(stype, awards), rlang::quos(NULL))
)
Run the code above in your browser using DataLab