Skip to contents

This function creates date(s) as output from datetime variable(s)

Usage

derive_vars_dtm_to_dt(dataset, source_vars)

Arguments

dataset

Input dataset

source_vars

A list of datetime variables created using vars() from which dates are to be extracted

Value

A data frame containing the input dataset with the corresponding date (--DT) variable(s) of all datetime variables (--DTM) specified in source_vars.

See also

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

Author

Teckla Akinyi

Examples

library(dplyr, warn.conflicts = FALSE)
library(lubridate)

adcm <- tibble::tribble(
  ~USUBJID, ~TRTSDTM,              ~ASTDTM,               ~AENDTM,
  "PAT01",  "2012-02-25 23:00:00", "2012-02-28 19:00:00", "2012-02-25 23:00:00",
  "PAT01",  NA,                    "2012-02-28 19:00:00", NA,
  "PAT01",  "2017-02-25 23:00:00", "2013-02-25 19:00:00", "2014-02-25 19:00:00",
  "PAT01",  "2017-02-25 16:00:00", "2017-02-25 14:00:00", "2017-03-25 23:00:00",
  "PAT01",  "2017-02-25 16:00:00", "2017-02-25 14:00:00", "2017-04-29 14:00:00",
) %>%
  mutate(
    TRTSDTM = as_datetime(TRTSDTM),
    ASTDTM = as_datetime(ASTDTM),
    AENDTM = as_datetime(AENDTM)
  )

adcm %>%
  derive_vars_dtm_to_dt(vars(TRTSDTM, ASTDTM, AENDTM)) %>%
  select(USUBJID, starts_with("TRT"), starts_with("AST"), starts_with("AEN"))
#> # A tibble: 5 x 7
#>   USUBJID TRTSDTM             TRTSDT     ASTDTM              ASTDT     
#>   <chr>   <dttm>              <date>     <dttm>              <date>    
#> 1 PAT01   2012-02-25 23:00:00 2012-02-25 2012-02-28 19:00:00 2012-02-28
#> 2 PAT01   NA                  NA         2012-02-28 19:00:00 2012-02-28
#> 3 PAT01   2017-02-25 23:00:00 2017-02-25 2013-02-25 19:00:00 2013-02-25
#> 4 PAT01   2017-02-25 16:00:00 2017-02-25 2017-02-25 14:00:00 2017-02-25
#> 5 PAT01   2017-02-25 16:00:00 2017-02-25 2017-02-25 14:00:00 2017-02-25
#> # … with 2 more variables: AENDTM <dttm>, AENDT <date>