Create factor variables based on decodes
Arguments
- .data
data frame
- decode_tbls
decode_tbls
list as returned byextract_decode_tbls()
orextract_decode_tbls_from_data()
. Alternatively, the result ofread_requirements()
can be used directly.- new_names
NULL
to update in-place or a namedlist
, where elements correspond to matching names in.data
anddecode_tbls
. One element can be left unnamed to provide the default transformation. Each element should be either: (1) a function/formula; (2) a custom glue specification, like"{var}CAT"
to create a new variable that is the original variable name appended with "CAT" for categorical. The default isNULL
, to update in-place.- ...
additional arguments (currently unused)
Value
data frame with the same number of rows as .data
. New/modified
variables will be included for matching content in .data
and
decode_tbls
.
Examples
# create factors in-place
dmcognigen_cov |>
set_decode_factors(dmcognigen_pk_requirements) |>
cnt(SEXF, RACEN)
#> ✔ Modified variable `NCILIV` as a factor of `NCILIV`.
#> ✔ Modified variable `RACEN` as a factor of `RACEN`.
#> ✔ Modified variable `RFCAT` as a factor of `RFCAT`.
#> ✔ Modified variable `SEXF` as a factor of `SEXF`.
#> # A tibble: 5 × 4
#> SEXF RACEN n n_cumulative
#> <fct> <fct> <int> <int>
#> 1 Male White/Caucasian 104 104
#> 2 Male Black/African American 6 110
#> 3 Male American Indian or Alaska Native 1 111
#> 4 Female White/Caucasian 126 237
#> 5 Female Black/African American 17 254
# or use a mapping convention to define new names
dmcognigen_cov |>
cnt(SEXF, RACEN) |>
set_decode_factors(dmcognigen_pk_requirements, new_names = c(SEXF = "SEXFCAT"))
#> ✔ Created new variable `SEXFCAT` as a factor of `SEXF`.
#> # A tibble: 5 × 5
#> SEXF RACEN n n_cumulative SEXFCAT
#> <dbl> <dbl> <int> <int> <fct>
#> 1 0 1 104 104 Male
#> 2 0 2 6 110 Male
#> 3 0 4 1 111 Male
#> 4 1 1 126 237 Female
#> 5 1 2 17 254 Female
dmcognigen_cov |>
cnt(SEXF, RACEN) |>
set_decode_factors(dmcognigen_pk_requirements, new_names = "{var}CAT")
#> ✔ Created new variable `RACENCAT` as a factor of `RACEN`.
#> ✔ Created new variable `SEXFCAT` as a factor of `SEXF`.
#> # A tibble: 5 × 6
#> SEXF RACEN n n_cumulative RACENCAT SEXFCAT
#> <dbl> <dbl> <int> <int> <fct> <fct>
#> 1 0 1 104 104 White/Caucasian Male
#> 2 0 2 6 110 Black/African American Male
#> 3 0 4 1 111 American Indian or Alaska Native Male
#> 4 1 1 126 237 White/Caucasian Female
#> 5 1 2 17 254 Black/African American Female