Skip to contents

Merge user-defined lookup table with the input dataset. Optionally print a list of records from the input dataset that do not have corresponding mapping from the lookup table.

Usage

derive_vars_merged_lookup(
  dataset,
  dataset_add,
  by_vars,
  order = NULL,
  new_vars = NULL,
  mode = NULL,
  filter_add = NULL,
  check_type = "warning",
  duplicate_msg = NULL,
  print_not_mapped = TRUE
)

Arguments

dataset

Input dataset

The variables specified by the by_vars argument are expected.

dataset_add

Lookup table

The variables specified by the by_vars argument are expected.

by_vars

Grouping variables

The input dataset and the selected observations from the additional dataset are merged by the specified by variables. The by variables must be a unique key of the selected observations. Variables from the additional dataset can be renamed by naming the element, i.e., by_vars = exprs(<name in input dataset> = <name in additional dataset>), similar to the dplyr joins.

Permitted Values: list of variables created by exprs()

order

Sort order

If the argument is set to a non-null value, for each by group the first or last observation from the additional dataset is selected with respect to the specified order.

Default: NULL

Permitted Values: list of variables or desc(<variable>) function calls created by exprs(), e.g., exprs(ADT, desc(AVAL)) or NULL

new_vars

Variables to add

The specified variables from the additional dataset are added to the output dataset. Variables can be renamed by naming the element, i.e., new_vars = exprs(<new name> = <old name>).

For example new_vars = exprs(var1, var2) adds variables var1 and var2 from dataset_add to the input dataset.

And new_vars = exprs(var1, new_var2 = old_var2) takes var1 and old_var2 from dataset_add and adds them to the input dataset renaming old_var2 to new_var2.

If the argument is not specified or set to NULL, all variables from the additional dataset (dataset_add) are added.

Default: NULL

Permitted Values: list of variables created by exprs()

mode

Selection mode

Determines if the first or last observation is selected. If the order argument is specified, mode must be non-null.

If the order argument is not specified, the mode argument is ignored.

Default: NULL

Permitted Values: "first", "last", NULL

filter_add

Filter for additional dataset (dataset_add)

Only observations fulfilling the specified condition are taken into account for merging. If the argument is not specified, all observations are considered.

Default: NULL

Permitted Values: a condition

check_type

Check uniqueness?

If "warning" or "error" is specified, the specified message is issued if the observations of the (restricted) additional dataset are not unique with respect to the by variables and the order.

Default: "warning"

Permitted Values: "none", "warning", "error"

duplicate_msg

Message of unique check

If the uniqueness check fails, the specified message is displayed.

Default:

paste("Dataset `dataset_add` contains duplicate records with respect to",
      enumerate(vars2chr(by_vars)))

print_not_mapped

Print a list of unique by_vars values that do not have corresponding records from the lookup table?

Default: TRUE

Permitted Values: TRUE, FALSE

Value

The output dataset contains all observations and variables of the input dataset, and add the variables specified in new_vars from the lookup table specified in dataset_add. Optionally prints a list of unique by_vars values that do not have corresponding records from the lookup table (by specifying print_not_mapped = TRUE).

Examples

library(admiral.test)
library(tibble)
library(dplyr, warn.conflicts = FALSE)
data("admiral_vs")
param_lookup <- tribble(
  ~VSTESTCD, ~VSTEST, ~PARAMCD, ~PARAM,
  "SYSBP", "Systolic Blood Pressure", "SYSBP", "Systolic Blood Pressure (mmHg)",
  "WEIGHT", "Weight", "WEIGHT", "Weight (kg)",
  "HEIGHT", "Height", "HEIGHT", "Height (cm)",
  "TEMP", "Temperature", "TEMP", "Temperature (C)",
  "MAP", "Mean Arterial Pressure", "MAP", "Mean Arterial Pressure (mmHg)",
  "BMI", "Body Mass Index", "BMI", "Body Mass Index(kg/m^2)",
  "BSA", "Body Surface Area", "BSA", "Body Surface Area(m^2)"
)
derive_vars_merged_lookup(
  dataset = admiral_vs,
  dataset_add = param_lookup,
  by_vars = exprs(VSTESTCD),
  new_vars = exprs(PARAMCD),
  print_not_mapped = TRUE
)
#> List of `VSTESTCD` not mapped: 
#> # A tibble: 2 x 1
#>   VSTESTCD
#>   <chr>   
#> 1 DIABP   
#> 2 PULSE   
#> Run `get_not_mapped()` to access the full list
#> # A tibble: 29,643 x 25
#>    STUDYID  DOMAIN USUBJID VSSEQ VSTESTCD VSTEST VSPOS VSORRES VSORRESU VSSTRESC
#>    <chr>    <chr>  <chr>   <dbl> <chr>    <chr>  <chr> <chr>   <chr>    <chr>   
#>  1 CDISCPI… VS     01-701…     1 DIABP    Diast… SUPI… 64      mmHg     64      
#>  2 CDISCPI… VS     01-701…     2 DIABP    Diast… STAN… 83      mmHg     83      
#>  3 CDISCPI… VS     01-701…     3 DIABP    Diast… STAN… 57      mmHg     57      
#>  4 CDISCPI… VS     01-701…     4 DIABP    Diast… SUPI… 68      mmHg     68      
#>  5 CDISCPI… VS     01-701…     5 DIABP    Diast… STAN… 59      mmHg     59      
#>  6 CDISCPI… VS     01-701…     6 DIABP    Diast… STAN… 71      mmHg     71      
#>  7 CDISCPI… VS     01-701…     7 DIABP    Diast… SUPI… 56      mmHg     56      
#>  8 CDISCPI… VS     01-701…     8 DIABP    Diast… STAN… 51      mmHg     51      
#>  9 CDISCPI… VS     01-701…     9 DIABP    Diast… STAN… 61      mmHg     61      
#> 10 CDISCPI… VS     01-701…    10 DIABP    Diast… SUPI… 67      mmHg     67      
#> # … with 29,633 more rows, and 15 more variables: VSSTRESN <dbl>,
#> #   VSSTRESU <chr>, VSSTAT <chr>, VSLOC <chr>, VSBLFL <chr>, VISITNUM <dbl>,
#> #   VISIT <chr>, VISITDY <dbl>, VSDTC <chr>, VSDY <dbl>, VSTPT <chr>,
#> #   VSTPTNUM <dbl>, VSELTM <chr>, VSTPTREF <chr>, PARAMCD <chr>