R/expandTables.R

Defines functions expandTables

Documented in expandTables

#' @title Convert \code{2x2 tables} to \code{data.frame}
#'
#' @description
#' \code{expandTables} generates a data frame and supports two levels.
#' @param ... vectors of 2x2 tables
#' @param exp_name Name of \code{Exposure} Variable
#' @param exp_lvl Names of two categories in the order of
#' Exposed and non-exposed
#' @param case_name Name of \code{Case} variable
#' @param case_lvl names of two categories in the order of
#' @param strata_name Name of stratified variable
#' @param stringsAsFactors logical: should the character vector be
#' converted to a factor?
#' Disease and healthy
#' @details
#'
#' \code{expandTables}
#' uses the vectors of 2x2 tables and generates a data frame of three
#' columns: Exposure, Outcome and Strata, intended for calculations
#' of stratified 2x2 tables using Mantel Haenszel methods.
#'
#' @seealso \code{\link{mhodds}}, \code{\link{stmh}}
#' @keywords expand tables, two-by-two tables, contigency tables
#' @author Myo Minn Oo (Email: \email{dr.myominnoo@@gmail.com} |
#' Website: \url{https://myominnoo.github.io/})
#' @examples
#' \dontrun{
#'
#' ## one table
#' tab <- expandTables(c(60, 140, 60, 40))
#' xtab(tab, exposure)
#'
#' ## one table with full labels
#' tab <- expandTables(c(60, 140, 60, 40), exp_name = "Type of area",
#'     exp_lvl = c("Rural", "urban"),
#'     case_name = "Antibodies",
#'     case_lvl = c("Yes", "No"))
#' xtab(tab)
#'
#' ## 2 tables
#' tab <- expandTables(male = c(36, 14, 50, 50),
#'     female = c(24, 126, 10, 90),
#'     exp_name = "Type of area", exp_lvl = c("Rural", "Urban"),
#'     case_name = "Antibodies", case_lvl = c("Yes", "No"),
#'     strata_name = "gender")
#' ### checking numbers
#' tab %>%
#'     pick(gender == "male") %>%
#'     xtab(`Type of area`, `Antibodies`, .)
#' tab %>%
#'     pick(gender == "female") %>%
#'     xtab(`Type of area`, `Antibodies`, .)
#' }


#' @export
expandTables <- function( ... ,
                          exp_name = "exposure",
                          exp_lvl = c("exposed", "unexposed"),
                          case_name = "outcome",
                          case_lvl = c("disease", "healthy"),
                          strata_name = "strata",
                          stringsAsFactors = FALSE)
{
  arguments <- as.list(match.call())
  arguments <- arguments[-1]
  arg_name <- names(arguments)
  extraArg <- c("exp_name", "exp_lvl", "case_name", "case_lvl",
                "strata_name")
  if (any(arg_name %in% extraArg)) {
    arguments <- arguments[!(arg_name %in% extraArg)]
  }
  strata_lvl <- names(arguments)
  if (is.null(strata_lvl) | length(strata_lvl) < 2) {
    times <- as.numeric(as.character(arguments[[1]])[-1])
    exp <- c(exp_lvl[1], exp_lvl[1], exp_lvl[2], exp_lvl[2])
    case <- c(case_lvl[1], case_lvl[2], case_lvl[1],
              case_lvl[2])
    t <- as.data.frame(
      cbind("exposure" = rep(exp, times),
            "case" = rep(case, times)),
      stringsAsFactors = stringsAsFactors)
    names(t) <- c(exp_name, case_name)
  } else {
    t <- do.call(
      rbind,
      lapply(strata_lvl, function(z) {
        times <- as.numeric(as.character(arguments[[z]])[-1])
        exp <- c(exp_lvl[1], exp_lvl[1], exp_lvl[2], exp_lvl[2])
        case <- c(case_lvl[1], case_lvl[2], case_lvl[1],
                  case_lvl[2])
        as.data.frame(
          cbind("exposure" = rep(exp, times),
                "case" = rep(case, times),
                "strata" = z),
          stringsAsFactors = stringsAsFactors)
      })
    )
    names(t) <- c(exp_name, case_name, strata_name)
  }

  return(t)
}
str(expandTables(c(60, 140, 60, 40)))
myominnoo/mStats_beta documentation built on Feb. 29, 2020, 8:17 a.m.