Skip to contents

Survey scales (education, Likert, frequency, ...) have a natural order that is tedious to retype as levels = c(...) every time. Register an order once, link it to the variable names and/or column prefixes it applies to, and the tabulating helpers (e.g. calc_percentage()) will look it up and apply it automatically. Persist your orders in a profile with save_ezrsurvey_profile().

Usage

register_order(name, levels, vars = NULL, prefixes = NULL, overwrite = TRUE)

Arguments

name

Short name for the order (e.g. "education").

levels

Character vector of the levels in ascending order.

vars

Optional exact column names this order applies to (e.g. "demo_edu").

prefixes

Optional column-name prefixes this order applies to (e.g. "edu_"). A variable matches if its name starts with any prefix.

overwrite

Overwrite an existing order of the same name. Default TRUE.

Value

Invisibly the order name.

Details

An order links a set of levels to the columns it applies to, by exact name (vars) and/or by prefix (prefixes). Once registered, calc_percentage(), calc_percentage_multi() and crosstab() look it up automatically (via order_for()) whenever you don't pass an explicit levels/sort, so a question always comes out in the right order without retyping it. Register interactively for the session, or store orders in a profile with save_ezrsurvey_profile() so they load every session.

Examples

register_order(
  "education",
  levels = c("Primary or less", "Lower secondary", "Upper secondary",
             "Short-cycle tertiary", "Bachelor or equivalent",
             "Master or equivalent", "Doctoral or equivalent"),
  vars = "demo_edu"
)
calc_percentage(podracing_survey, demo_edu)   # rows now in education order
#> # A tibble: 7 × 3
#>   demo_edu                   n   pct
#>   <ord>                  <int> <dbl>
#> 1 Primary or less           42     4
#> 2 Lower secondary          111    11
#> 3 Upper secondary          295    30
#> 4 Short-cycle tertiary     123    13
#> 5 Bachelor or equivalent   244    25
#> 6 Master or equivalent     122    13
#> 7 Doctoral or equivalent    35     4
remove_order("education")