This functions uses code/decode pairs from a metacore object to create new variables in the data
Arguments
- data
Dataset that contains the input variable
- metacore
A metacore object to get the codelist from. If the `out_var` has different codelists for different datasets the metacore object will need to be subsetted using `select_dataset` from the metacore package.
- input_var
Name of the variable that will be translated for the new column
- out_var
Name of the output variable. Note: the grouping will always be from the code of the codelist associates with `out_var`
- decode_to_code
Direction of the translation. By default assumes the `input_var` is the decode column of the codelist. Set to `FALSE` if the `input_var` is the code column of the codelist
Examples
library(metacore)
library(tibble)
data <- tribble(
~USUBJID, ~VAR1, ~VAR2,
1, "M", "Male",
2, "F", "Female",
3, "F", "Female",
4, "U", "Unknown",
5, "M", "Male",
)
spec <- spec_to_metacore(metacore_example("p21_mock.xlsx"), quiet = TRUE)
#>
#> Metadata successfully imported
#> Loading in metacore object with suppressed warnings
create_var_from_codelist(data, spec, VAR2, SEX)
#> # A tibble: 5 × 4
#> USUBJID VAR1 VAR2 SEX
#> <dbl> <chr> <chr> <chr>
#> 1 1 M Male M
#> 2 2 F Female F
#> 3 3 F Female F
#> 4 4 U Unknown U
#> 5 5 M Male M
create_var_from_codelist(data, spec, "VAR2", "SEX")
#> # A tibble: 5 × 4
#> USUBJID VAR1 VAR2 SEX
#> <dbl> <chr> <chr> <chr>
#> 1 1 M Male M
#> 2 2 F Female F
#> 3 3 F Female F
#> 4 4 U Unknown U
#> 5 5 M Male M
create_var_from_codelist(data, spec, VAR1, SEX, decode_to_code = FALSE)
#> # A tibble: 5 × 4
#> USUBJID VAR1 VAR2 SEX
#> <dbl> <chr> <chr> <chr>
#> 1 1 M Male Male
#> 2 2 F Female Female
#> 3 3 F Female Female
#> 4 4 U Unknown Unknown
#> 5 5 M Male Male