R/perc_n2.R

Defines functions perc_n2

Documented in perc_n2

#' Create descriptives for ategorical variables
#' Current version works in the html setting. We are working on extend it to the word document
#'
#' @param x Vector of a categorical variable
#' @return descriptive table
#' @examples
#' data(mtcars)
#' mtcars = mtcars %>% mutate(cyl=factor(cyl),vs=factor(vs), am=factor(am))
#' varlist=c('mpg','disp','hp','drat','wt','gear','carb','vs')
#' desc_table(varlist, data=mtcars,groupvar=NULL)
#' desc_table(varlist, data=mtcars,groupvar="cyl")
#' desc_table(varlist, data=mtcars,groupvar="cyl",total=FALSE)
#' @author Seonjoo Lee, \email{sl3670@cumc.columbia.edu}
#' @references TBA
#' @keywords highdimensional mediation L1penalization
#' @import dplyr
#' @import qwraps2
#' @import knitr
#' @export

perc_n2<-function(x, digits = getOption("qwraps2_frmt_digits", 1), na_rm = FALSE,
                   show_denom = "ifNA", markup = getOption("qwraps2_markup",
                                                           "latex"))
{
  d <- sum(!is.na(x))
  n <- sum(x, na.rm = na_rm)
  p <- frmt(100 * n/d, digits)
  if (show_denom == "never") {
    rtn <- paste0(frmt(as.integer(n)), " (", p, ")")
  }
  else {
    if (show_denom == "always" | any(is.na(x))) {
      rtn <- paste0(frmt(as.integer(n)), " (", p,
                    "non-missing)")
    }
    else {
      rtn <- paste0(frmt(as.integer(n)), " (", p,
                    ")")
    }
  }
  if (markup == "latex") {
    rtn <- gsub("%", "\\\\%", rtn)
  }
  return(rtn)
}
seonjoo/slmisc documentation built on Dec. 9, 2019, 7:47 p.m.