Skip to contents

Adds relative day variables (--DY) to the dataset, e.g., ASTDY and AENDY.

Usage

derive_vars_dy(dataset, reference_date, source_vars)

Arguments

dataset

Input dataset

The columns specified by the reference_date and the source_vars parameter are expected.

reference_date

The start date column, e.g., date of first treatment

A date or date-time object column is expected.

Refer to derive_vars_dt() to impute and derive a date from a date character vector to a date object.

source_vars

A list of datetime or date variables created using vars() from which dates are to be extracted. This can either be a list of date(time) variables or named --DY variables and corresponding --DT(M) variables e.g. vars(TRTSDTM, ASTDTM, AENDT) or vars(TRTSDT, ASTDTM, AENDT, DEATHDY = DTHDT). If the source variable does not end in --DT(M), a name for the resulting --DY variable must be provided.

Value

The input dataset with --DY corresponding to the --DTM or --DTsource variable(s) added

Details

The relative day is derived as number of days from the reference date to the end date. If it is nonnegative, one is added. I.e., the relative day of the reference date is 1. Unless a name is explicitly specified, the name of the resulting relative day variable is generated from the source variable name by replacing DT (or DTM as appropriate) with DY.

See also

Date/Time Derivation Functions that returns variable appended to dataset: derive_var_trtdurd(), derive_vars_dtm_to_dt(), derive_vars_dtm_to_tm(), derive_vars_dtm(), derive_vars_dt(), derive_vars_duration()

Author

Teckla Akinyi

Examples

library(lubridate)
library(dplyr)

datain <- tibble::tribble(
  ~TRTSDTM, ~ASTDTM, ~AENDT,
  "2014-01-17T23:59:59", "2014-01-18T13:09:O9", "2014-01-20"
) %>%
  mutate(
    TRTSDTM = as_datetime(TRTSDTM),
    ASTDTM = as_datetime(ASTDTM),
    AENDT = ymd(AENDT)
  )

derive_vars_dy(
  datain,
  reference_date = TRTSDTM,
  source_vars = vars(TRTSDTM, ASTDTM, AENDT)
)
#> # A tibble: 1 x 6
#>   TRTSDTM             ASTDTM              AENDT      TRTSDY ASTDY AENDY
#>   <dttm>              <dttm>              <date>      <dbl> <dbl> <dbl>
#> 1 2014-01-17 23:59:59 2014-01-18 13:09:09 2014-01-20      1     2     4

# specifying name of new variables
datain <- tibble::tribble(
  ~TRTSDT, ~DTHDT,
  "2014-01-17", "2014-02-01"
) %>%
  mutate(
    TRTSDT = ymd(TRTSDT),
    DTHDT = ymd(DTHDT)
  )

derive_vars_dy(
  datain,
  reference_date = TRTSDT,
  source_vars = vars(TRTSDT, DEATHDY = DTHDT)
)
#> # A tibble: 1 x 4
#>   TRTSDT     DTHDT      TRTSDY DEATHDY
#>   <date>     <date>      <dbl>   <dbl>
#> 1 2014-01-17 2014-02-01      1      16