benjaminrich / table1

78 stars 26 forks source link

Generate table with element not showed in the DF #108

Closed Qiyun506 closed 7 months ago

Qiyun506 commented 1 year ago

Hi, When I use the table1 pacakge to generate a shift table, to see how a var change after certain experiement. Subjects initial are normal, low abnormal, and abnormal, but for some sample, their initial status are all normal, as a result when use your table, the thing showed in table is not 0 but nothing. like this:

Mine: mine Expected: wish

Can anyone help me with that?

benjaminrich commented 9 months ago

Sorry for the delay in addressing your question. Although it could possibly be done somehow, table1 might not be the ideal package for this kind of table. If you want to try my other package ttt, you could do something like this:

library(ttt)
options(ttt.theme="booktabs")

lev <- c("Normal or Low", "Abnormal high, NCS", "Abnormal high, CS")

dat <- data.frame(pre=factor(c(rep(lev[1], 7), lev[2]), levels=lev))
dat$post <- dat$pre

shift_table <- function(pre, post, label) {

    tab <- table(pre, post)
    a <- attributes(tab)
    tab <- sprintf("%s (%s%%)", tab, 100*prop.table(tab))
    tab <- gsub(" (0%)", "", tab, fixed=T)
    attributes(tab) <- a

    tab <- as.data.frame(tab)

    tab <- rbind(
        data.frame(
            pre=factor(levels(pre), levels=levels(pre)),
            post=factor("&nbsp;"), Freq="&nbsp;"),
        tab)

    tab$label <- label

    label(tab$label) <- "Parameter (unit)"
    label(tab$post)  <- "Postbaseline"

    ttt(Freq ~ label + post | pre, data=tab, lab="Baseline")
}

shift_table(dat$pre, dat$post,
    label="Alanine<br/>Aminotransferase<br/>(ALT/SGPT) (U/L)")

image