#' Generate the survival data
#'
#' Generate a dataframe containing observed durations (surv) and censoring
#' indicators (cens) for each individual
#'
#' @param id ids in the longitudinal dataset
#' @param time observation times in the longitudinal dataset
#' @param cens censoring indicator in the longitudinal dataset (has to be
#' uniquely defined by id)
#'
#' @return a dataframe containing observed durations (surv) and censoring
#' indicators (cens) for each individual
#'
#' @examples
#' data(onco)
#' SurvDat(onco$id, onco$drel, onco$cens)
#'
#' @export
SurvDat <- function(id, time, cens) {
# look for last observation time for each id
last <- aggregate(time, list(id), max)
names(last) <- c("id", "surv")
# add one unit to get survival time
last$surv <- last$surv + 1
# check that censoring indicator is uniquely defined for each id
if(any(aggregate(cens, list(id), function(z) length(unique(z)) > 1)[[2]])) {
stop("censoring indicator is not uniquely defined for each id")
}
# get censoring indicator for each id
ind <- aggregate(cens, list(id), unique)
names(ind) <- c("id", "cens")
# merge survival time and censoring indicator
surv <- merge(last, ind, by = "id")
return(surv)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.