surv_time.Rd
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).
surv_time( .data, start_time, event, event_date, fu_date, surv_time = "surv_time", surv_end_time = "surv_end_time" )
.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. |
# 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)#> # A tibble: 10 x 6 #> diagnosis_dt death fu_dt death_dt surv_end_time surv_time #> <date> <dbl> <date> <date> <date> <dbl> #> 1 1986-06-08 0 2009-11-13 NA 2009-11-13 23.4 #> 2 1988-01-05 0 2009-11-13 NA 2009-11-13 21.9 #> 3 1986-03-15 0 2009-11-13 NA 2009-11-13 23.7 #> 4 1985-05-22 0 2009-11-13 NA 2009-11-13 24.5 #> 5 1989-12-14 0 2009-11-13 NA 2009-11-13 19.9 #> 6 1988-05-01 0 2009-11-13 NA 2009-11-13 21.5 #> 7 1989-12-19 1 2009-11-13 2011-03-17 2011-03-17 21.2 #> 8 1989-08-26 1 2009-11-13 2017-04-26 2017-04-26 27.7 #> 9 1986-05-18 0 2009-11-13 NA 2009-11-13 23.5 #> 10 1986-04-20 1 2009-11-13 2012-09-22 2012-09-22 26.4