Skip to contents

Takes one categorical variable and returns a tidy dataframe, including relative frequencies and absolute size of each group. The variable can be further segmented by another categorical variable. Can also take survey weights. Returns 95% confidence intervals.

Usage

stem_summarise_cat(
  data,
  item,
  group = NULL,
  weight = NULL,
  long = FALSE,
  collapse_item = NULL,
  collapse_group = NULL,
  return_n = FALSE
)

Arguments

data

Dataframe including variables to be analyzed

item

Variable to be summarised

group

Optional segmenting variable

weight

Optional survey weights

long

Returns data in long format. Useful if multiple dataframes are to be merged

collapse_item

Named list. Optionally collapes (or renames) categories of the item variable

collapse_group

Named list. Optionally collapes (or renames) categories of the group variable

return_n

If TRUE, returns absolute group sizes.

Value

Tidy dataframe

Examples

trust |> stem_summarise_cat(item = police,
                            group = eu,
                            weight = W,
                            collapse_item = list(Agree = c("Definitely Agree", "Rather Agree")))
#> # A tibble: 20 × 5
#>    eu                         police                      freq freq_low freq_upp
#>    <fct>                      <fct>                      <dbl>    <dbl>    <dbl>
#>  1 Definitely Agree           Agree                     0.733   0.608     0.830 
#>  2 Definitely Agree           Neither Agree nor Disagr… 0.104   0.0499    0.203 
#>  3 Definitely Agree           Rather Disagree           0.0846  0.0385    0.176 
#>  4 Definitely Agree           Definitely Disagree       0.0783  0.0277    0.202 
#>  5 Rather Agree               Agree                     0.702   0.598     0.790 
#>  6 Rather Agree               Neither Agree nor Disagr… 0.197   0.127     0.293 
#>  7 Rather Agree               Rather Disagree           0.0320  0.0159    0.0632
#>  8 Rather Agree               Definitely Disagree       0.0688  0.0261    0.169 
#>  9 Neither Agree nor Disagree Agree                     0.720   0.652     0.779 
#> 10 Neither Agree nor Disagree Neither Agree nor Disagr… 0.152   0.109     0.208 
#> 11 Neither Agree nor Disagree Rather Disagree           0.0720  0.0412    0.123 
#> 12 Neither Agree nor Disagree Definitely Disagree       0.0555  0.0301    0.0999
#> 13 Rather Disagree            Agree                     0.750   0.663     0.821 
#> 14 Rather Disagree            Neither Agree nor Disagr… 0.146   0.0913    0.224 
#> 15 Rather Disagree            Rather Disagree           0.0436  0.0197    0.0937
#> 16 Rather Disagree            Definitely Disagree       0.0606  0.0291    0.122 
#> 17 Definitely Disagree        Agree                     0.685   0.543     0.800 
#> 18 Definitely Disagree        Neither Agree nor Disagr… 0.240   0.139     0.382 
#> 19 Definitely Disagree        Rather Disagree           0.0163  0.00228   0.108 
#> 20 Definitely Disagree        Definitely Disagree       0.0585  0.0183    0.172