R/table2df.r

Defines functions table2df table2dt

Documented in table2df table2dt

#' Convert a table to a (disaggregated) data frame / table
#'
#' Note that as.data.frame can do this, but you get aggregated rows with a `Freq` column
#'
#' @param Tab   The table
#'
#' @examples
#' table2dt(Titanic)
#' head(table2df(Titanic))
#'
#' @export

table2dt <- function(Tab) {
  tmp <- data.table::as.data.table(Tab)
  tmp[rep(1:nrow(tmp), tmp$N),-ncol(tmp)]
}

#' @describeIn table2dt   Convert a table to a (disaggregated) data frame
#' @export

table2df <- function(Tab) {
  tmp <- as.data.frame(Tab)
  tmp <- tmp[rep(1:nrow(tmp), tmp$Freq),-ncol(tmp)]
  rownames(tmp) <- NULL
  tmp
}
pbreheny/breheny documentation built on April 26, 2024, 10:40 a.m.