surv_time: Calculate time variable for survival analysis

View source: R/surv_time.R

surv_timeR Documentation

Calculate time variable for survival analysis

Description

Clinical data often has separate columns for event dates and last follow-up dates. This function calculates the time variable to be used in time-to-event analysis (i.e. time to event or time to censor).

Usage

surv_time(
  .data,
  start_time,
  event,
  event_date,
  fu_date,
  surv_time = "surv_time",
  surv_end_time = "surv_end_time"
)

Arguments

.data

A tbl

start_time

A column of dates or datetimes to use as the starting point. This will often be date of diagnosis.

event

A column of numeric indicators (0,1) where 1 represents occurrence of the event.

event_date

A column of dates or datetimes corresponding to the event.

fu_date

A column of dates or datetimes corresponding to the last known follow-up date.

surv_time

The name of the output column representing survival time

surv_end_time

The name of the output column representing the last date to be used in survival. This will either be event_date or fu_date.

Examples

# Create some test data
set.seed(12215)

surv_data <- tibble::tibble(
  diagnosis_dt = sample(seq(as.Date("1985/01/01"), as.Date("1990/01/01"), by = "day"), 10),
  death = sample(c(0, 1), 10, replace = TRUE),
  fu_dt = as.Date("2009/11/13")
)

surv_data <- surv_data %>%
  dplyr::rowwise() %>%
  dplyr::mutate(death_dt = dplyr::if_else(death == 1,
       sample(seq(as.Date("2010/01/01"), as.Date("2019/01/01"), by = "day"), 1),
       as.Date(NA))) %>%
  dplyr::ungroup()

surv_data %>%
  surv_time(diagnosis_dt, death, death_dt, fu_dt)

overdodactyl/mRclwhip documentation built on June 30, 2023, 6:24 a.m.