Skip to contents

Assign values to a related variable within a domain.

Usage

rel_var(df, var_name, related_var, var_values = NULL)

Arguments

df

(data.frame)
Data frame containing the related variables.

var_name

(character)
Name of variable related to rel_var to add to df.

(character)
Name of variable within df with values to which values of var_name must relate.

var_values

(any)
Vector of values related to values of related_var.

Value

df with added factor variable var_name containing var_values corresponding to related_var.

Examples

# Example with data.frame.
params <- c("Level A", "Level B", "Level C")
adlb_df <- data.frame(
  ID = 1:9,
  PARAM = factor(
    rep(c("Level A", "Level B", "Level C"), 3),
    levels = params
  )
)
rel_var(
  df = adlb_df,
  var_name = "PARAMCD",
  var_values = c("A", "B", "C"),
  related_var = "PARAM"
)
#>   ID   PARAM PARAMCD
#> 1  1 Level A       A
#> 2  2 Level B       B
#> 3  3 Level C       C
#> 4  4 Level A       A
#> 5  5 Level B       B
#> 6  6 Level C       C
#> 7  7 Level A       A
#> 8  8 Level B       B
#> 9  9 Level C       C

# Example with tibble.
adlb_tbl <- tibble::tibble(
  ID = 1:9,
  PARAM = factor(
    rep(c("Level A", "Level B", "Level C"), 3),
    levels = params
  )
)
rel_var(
  df = adlb_tbl,
  var_name = "PARAMCD",
  var_values = c("A", "B", "C"),
  related_var = "PARAM"
)
#> # A tibble: 9 × 3
#>      ID PARAM   PARAMCD
#>   <int> <fct>   <fct>  
#> 1     1 Level A A      
#> 2     2 Level B B      
#> 3     3 Level C C      
#> 4     4 Level A A      
#> 5     5 Level B B      
#> 6     6 Level C C      
#> 7     7 Level A A      
#> 8     8 Level B B      
#> 9     9 Level C C