R/as_data_frame_table1.R

Defines functions as_data_frame_table1

Documented in as_data_frame_table1

#' Convert table1 into data.frame
#'
#' @param x table1 object
#' @param ... additional arguments (see table1 documentation)
#' @export

as_data_frame_table1 <- function(x, ...) {
  obj <- attr(x, "obj")
  with(obj, {
    rlh <- if (is.null(rowlabelhead) || rowlabelhead=="") " " else rowlabelhead
    z <- lapply(contents, function(y) {
      y <- as.data.frame(y, stringsAsFactors=F)
      y2 <- data.frame(x=paste0(c("", rep("  ", nrow(y) - 1)), rownames(y)), stringsAsFactors=F)
      y <- cbind(setNames(y2, rlh), y)
      y
    })
    df <- do.call(rbind, z)
    df <- rbind(c("", ifelse(is.na(headings[2,]), "", sprintf("(N=%s)", headings[2,]))), df)
    colnames(df) <- c(rlh, headings[1,])
    rownames(df) <- NULL
    noquote(df)
  })
}
MBender1992/emR documentation built on Feb. 18, 2025, 9:21 a.m.