Skip to contents

Merge a character variable from a dataset to the input dataset. The observations to merge can be selected by a condition and/or selecting the first or last observation for each by group.

Usage

derive_var_merged_character(
  dataset,
  dataset_add,
  by_vars,
  order = NULL,
  new_var,
  source_var,
  case = NULL,
  filter_add = NULL,
  mode = NULL,
  missing_value = NA_character_
)

Arguments

dataset

Input dataset

The variables specified by the by_vars argument are expected.

dataset_add

Additional dataset

The variables specified by the by_vars, the source_var, and the order 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_var

New variable

The specified variable is added to the additional dataset and set to the transformed value with respect to the case argument.

source_var

Source variable

case

Change case

Changes the case of the values of the new variable.

Default: NULL

Permitted Values: NULL, "lower", "upper", "title"

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

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

missing_value

Values used for missing information

The new variable is set to the specified value for all by groups without observations in the additional dataset.

Default: NA_character_

Permitted Value: A character scalar

Value

The output dataset contains all observations and variables of the input dataset and additionally the variable specified for new_var derived from the additional dataset (dataset_add).

Details

  1. The additional dataset is restricted to the observations matching the filter_add condition.

  2. The (transformed) character variable is added to the additional dataset.

  3. If order is specified, for each by group the first or last observation (depending on mode) is selected.

  4. The character variable is merged to the input dataset.

Examples

library(admiral.test)
library(dplyr, warn.conflicts = FALSE)
data("admiral_dm")
data("admiral_ds")

derive_var_merged_character(
  admiral_dm,
  dataset_add = admiral_ds,
  by_vars = exprs(STUDYID, USUBJID),
  new_var = DISPSTAT,
  filter_add = DSCAT == "DISPOSITION EVENT",
  source_var = DSDECOD,
  case = "title"
) %>%
  select(STUDYID, USUBJID, AGE, AGEU, DISPSTAT)
#> # A tibble: 306 x 5
#>    STUDYID      USUBJID       AGE AGEU  DISPSTAT                   
#>    <chr>        <chr>       <dbl> <chr> <chr>                      
#>  1 CDISCPILOT01 01-701-1015    63 YEARS Completed                  
#>  2 CDISCPILOT01 01-701-1023    64 YEARS Adverse Event              
#>  3 CDISCPILOT01 01-701-1028    71 YEARS Completed                  
#>  4 CDISCPILOT01 01-701-1033    74 YEARS Study Terminated By Sponsor
#>  5 CDISCPILOT01 01-701-1034    77 YEARS Completed                  
#>  6 CDISCPILOT01 01-701-1047    85 YEARS Adverse Event              
#>  7 CDISCPILOT01 01-701-1057    59 YEARS Screen Failure             
#>  8 CDISCPILOT01 01-701-1097    68 YEARS Completed                  
#>  9 CDISCPILOT01 01-701-1111    81 YEARS Adverse Event              
#> 10 CDISCPILOT01 01-701-1115    84 YEARS Adverse Event              
#> # … with 296 more rows