Skip to contents

Adds a flag variable to the input dataset which indicates if there exists at least one observation in another dataset fulfilling a certain condition.

Usage

derive_var_merged_exist_flag(
  dataset,
  dataset_add,
  by_vars,
  new_var,
  condition,
  true_value = "Y",
  false_value = NA_character_,
  missing_value = NA_character_,
  filter_add = NULL
)

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 argument are expected.

by_vars

Grouping variables

Permitted Values: list of variables

new_var

New variable

The specified variable is added to the input dataset.

condition

Condition

The condition is evaluated at the additional dataset (dataset_add). For all by groups where it evaluates as TRUE at least once the new variable is set to the true value (true_value). For all by groups where it evaluates as FALSE or NA for all observations the new variable is set to the false value (false_value). The new variable is set to the missing value (missing_value) for by groups not present in the additional dataset.

true_value

True value

Default: "Y"

false_value

False value

Default: NA_character_

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

filter_add

Filter for additional data

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

Permitted Values: a condition

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 new variable is added to the input dataset and set to the true value (true_value) if for the by group at least one observation exists in the (restricted) additional dataset where the condition evaluates to TRUE. It is set to the false value (false_value) if for the by group at least one observation exists and for all observations the condition evaluates to FALSE or NA. Otherwise, it is set to the missing value (missing_value).

Examples


library(admiral.test)
library(dplyr, warn.conflicts = FALSE)
data("admiral_dm")
data("admiral_ae")
derive_var_merged_exist_flag(
  admiral_dm,
  dataset_add = admiral_ae,
  by_vars = exprs(STUDYID, USUBJID),
  new_var = AERELFL,
  condition = AEREL == "PROBABLE"
) %>%
  select(STUDYID, USUBJID, AGE, AGEU, AERELFL)
#> # A tibble: 306 x 5
#>    STUDYID      USUBJID       AGE AGEU  AERELFL
#>    <chr>        <chr>       <dbl> <chr> <chr>  
#>  1 CDISCPILOT01 01-701-1015    63 YEARS Y      
#>  2 CDISCPILOT01 01-701-1023    64 YEARS Y      
#>  3 CDISCPILOT01 01-701-1028    71 YEARS Y      
#>  4 CDISCPILOT01 01-701-1033    74 YEARS NA     
#>  5 CDISCPILOT01 01-701-1034    77 YEARS Y      
#>  6 CDISCPILOT01 01-701-1047    85 YEARS NA     
#>  7 CDISCPILOT01 01-701-1057    59 YEARS NA     
#>  8 CDISCPILOT01 01-701-1097    68 YEARS Y      
#>  9 CDISCPILOT01 01-701-1111    81 YEARS NA     
#> 10 CDISCPILOT01 01-701-1115    84 YEARS Y      
#> # … with 296 more rows

data("admiral_vs")
derive_var_merged_exist_flag(
  admiral_dm,
  dataset_add = admiral_vs,
  by_vars = exprs(STUDYID, USUBJID),
  filter_add = VSTESTCD == "WEIGHT" & VSBLFL == "Y",
  new_var = WTBLHIFL,
  condition = VSSTRESN > 90,
  false_value = "N",
  missing_value = "M"
) %>%
  select(STUDYID, USUBJID, AGE, AGEU, WTBLHIFL)
#> # A tibble: 306 x 5
#>    STUDYID      USUBJID       AGE AGEU  WTBLHIFL
#>    <chr>        <chr>       <dbl> <chr> <chr>   
#>  1 CDISCPILOT01 01-701-1015    63 YEARS N       
#>  2 CDISCPILOT01 01-701-1023    64 YEARS N       
#>  3 CDISCPILOT01 01-701-1028    71 YEARS Y       
#>  4 CDISCPILOT01 01-701-1033    74 YEARS N       
#>  5 CDISCPILOT01 01-701-1034    77 YEARS N       
#>  6 CDISCPILOT01 01-701-1047    85 YEARS N       
#>  7 CDISCPILOT01 01-701-1057    59 YEARS M       
#>  8 CDISCPILOT01 01-701-1097    68 YEARS N       
#>  9 CDISCPILOT01 01-701-1111    81 YEARS N       
#> 10 CDISCPILOT01 01-701-1115    84 YEARS N       
#> # … with 296 more rows