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.

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).

Value

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

Details

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

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())
#> # A tibble: 6 × 4
#>   stype awards   prop prop_se
#>   <fct> <fct>   <dbl>   <dbl>
#> 1 E     No     0.193  0.0318 
#> 2 E     Yes    0.521  0.0318 
#> 3 H     No     0.0829 0.00812
#> 4 H     Yes    0.0390 0.00812
#> 5 M     No     0.0855 0.0117 
#> 6 M     Yes    0.0789 0.0117 

# But if you didn't interact, the sum of each stype's prop is 100%
dstrata %>%
  group_by(stype, awards) %>%
  summarize(prop = survey_mean())
#> # A tibble: 6 × 4
#> # Groups:   stype [3]
#>   stype awards  prop prop_se
#>   <fct> <fct>  <dbl>   <dbl>
#> 1 E     No     0.27   0.0446
#> 2 E     Yes    0.730  0.0446
#> 3 H     No     0.68   0.0666
#> 4 H     Yes    0.32   0.0666
#> 5 M     No     0.52   0.0714
#> 6 M     Yes    0.48   0.0714