interact: Create interaction terms to group by when summarizing

View source: R/interaction.R

interactR Documentation

Create interaction terms to group by when summarizing

Description

Allows multiple grouping by multiple variables as if they were a single variable, which allows calculating proportions that sum to 100 more than a single grouping variable with survey_mean.

Usage

interact(...)

Arguments

...

variables to group by. All types of tbls accept variable names, and most will also accept functions of variables (though some database-backed tbls do not allow creating variables).

Details

Behind the scenes, this function creates a special column type that is split back into the component columns automatically by summarize.

Value

A vector of type srvyr_interaction, which is generally expected to be automatically split apart.

Examples

data(api, package = "survey")

dstrata <- apistrat %>%
  as_survey_design(strata = stype, weights = pw)

# The sum of the whole prop column is equal to 100%
dstrata %>%
  group_by(interact(stype, awards)) %>%
  summarize(prop = survey_mean())

# But if you didn't interact, the sum of each stype's prop is 100%
dstrata %>%
  group_by(stype, awards) %>%
  summarize(prop = survey_mean())

srvyr documentation built on Sept. 11, 2024, 8:43 p.m.