R/coercion.R

Defines functions as_incidence.incidence2 as_incidence.default as_incidence as.data.frame.incidence2

Documented in as.data.frame.incidence2 as_incidence as_incidence.default as_incidence.incidence2

#' Convert incident object to a data frame
#'
#' @param x `<incidence2>` object.
#' @param row.names Not used.
#' @param optional Not used.
#' @param ... Not used.
#'
#' @examples
#' dat <- data.frame(
#'     dates = Sys.Date() + 1:100,
#'     names = rep(c("Jo", "John"), 5)
#' )
#'
#' dat <- incidence(dat, date_index = "dates", groups = "names")
#' as.data.frame(dat)
#'
#' @export
as.data.frame.incidence2 <- function(x, row.names, optional,...) {
    attributes(x) <- list(
        names = attr(x, "names"),
        row.names = .row_names_info(x, type = 0L),
        class = "data.frame"
    )
    x
}

#' Coerce to an incidence object
#'
#' Generic for coercion to an `<incidence2>` object.
#'
#' @param x An \R object.
#' @param ... Additional arguments to be passed to or from other methods.
#'
#' @return An `<incidence2>` object.
#'
#' @export
as_incidence <- function(x, ...) {
    UseMethod("as_incidence")
}

#' @rdname as_incidence
#' @export
as_incidence.default <- function(x, ...) {
    stopf("Not implemented for class [%s].", paste(class(x), collapse = ", "))
}

#' @rdname as_incidence
#' @export
as_incidence.incidence2 <- function(x, ...) {
    x
}

Try the incidence2 package in your browser

Any scripts or data that you put into this service are public.

incidence2 documentation built on July 9, 2023, 5:35 p.m.