Learn R Programming

srvyr (version 1.3.0)

interact: 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(...)

Value

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

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.

Examples

Run this code
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())

Run the code above in your browser using DataLab