convert_date_to_dtm: Convert a Date into a Datetime Object

Description Usage Arguments Value Author(s) Examples

View source: R/derive_date_vars.R

Description

Convert a date (datetime, date, or date character) into a Date vector (usually '--DTM').

Usage

1
2
3
4
5
6
7
8
convert_date_to_dtm(
  dt,
  date_imputation = NULL,
  time_imputation = NULL,
  min_dates = NULL,
  max_dates = NULL,
  preserve = FALSE
)

Arguments

dt

The date to convert.

A date or character date is expected in a format like yyyy-mm-ddThh:mm:ss.

date_imputation

The value to impute the day/month when a datepart is missing.

If NULL: no date imputation is performed and partial dates are returned as missing.

Otherwise, a character value is expected, either as a

  • format with month and day specified as "mm-dd": e.g. "06-15" for the 15th of June,

  • or as a keyword: "FIRST", "MID", "LAST" to impute to the first/mid/last day/month.

Default is NULL.

time_imputation

The value to impute the time when a timepart is missing.

A character value is expected, either as a

  • format with hour, min and sec specified as "hh:mm:ss": e.g. "00:00:00" for the start of the day,

  • or as a keyword: "FIRST","LAST" to impute to the start/end of a day.

Default is "00:00:00".

min_dates

Minimum dates

A list of dates is expected. It is ensured that the imputed date is not before any of the specified dates, e.g., that the imputed adverse event start date is not before the first treatment date. Only dates which are in the range of possible dates of the dtc value are considered. The possible dates are defined by the missing parts of the dtc date (see example below). This ensures that the non-missing parts of the dtc date are not changed. For example

impute_dtc(
  "2020-11",
  min_dates = list(
    ymd_hms("2020-12-06T12:12:12"),
    ymd_hms("2020-11-11T11:11:11")
   ),
  date_imputation = "first"
)

returns "2020-11-11T11:11:11" because the possible dates for "2020-11" range from "2020-11-01T00:00:00" to "2020-11-30T23:59:59". Therefore "2020-12-06T12:12:12" is ignored. Returning "2020-12-06T12:12:12" would have changed the month although it is not missing (in the dtc date).

max_dates

Maximum dates

A list of dates is expected. It is ensured that the imputed date is not after any of the specified dates, e.g., that the imputed date is not after the data cut off date. Only dates which are in the range of possible dates are considered.

preserve

Preserve day if month is missing and day is present

For example "2019---07" would return "2019-06-07 if preserve = TRUE (and date_imputation = "MID").

Permitted Values: TRUE, FALSE

Default: FALSE

Value

A datetime object

Author(s)

Samia Kabi

Examples

1
2
3
4
5
convert_date_to_dtm("2019-07-18T15:25:00")
convert_date_to_dtm(Sys.time())
convert_date_to_dtm(as.Date("2019-07-18"), time_imputation = "23:59:59")
convert_date_to_dtm("2019-07-18", time_imputation = "23:59:59")
convert_date_to_dtm("2019-07-18")

epijim/admiral documentation built on Feb. 13, 2022, 12:15 a.m.