Skip to contents

Builds a minimal `code`/`decode` codelist data.frame from the unique values of `data[[var]]`: `decode` is `sort(unique(...))` of the non-missing values and `code` is `seq_along(decode)`. The result is suitable as an entry of the `codelists` argument of [mock_metacore()], e.g. `codelists = list(RACE = codelist_from_data("RACE", dm))`.

Usage

codelist_from_data(var, data)

Arguments

var

Character, name of the column in `data` holding the decode values (e.g. `"RACE"`).

data

A data.frame containing `var`.

Value

A data.frame with columns `code` (integer) and `decode` (character), one row per unique non-missing value, sorted by decode.

Examples

dm <- data.frame(RACE = c("WHITE", "ASIAN", "BLACK", "ASIAN"))
codelist_from_data("RACE", dm)
#>   code decode
#> 1    1  ASIAN
#> 2    2  BLACK
#> 3    3  WHITE