benjaminrich / table1

78 stars 26 forks source link

Feature request: When summarizing counts, don't show the percent when the value is zero #104

Closed billdenney closed 1 year ago

billdenney commented 1 year ago

Currently, when a value is zero for a count, it shows as "0 (0%)". Could there be an option to make it just show "0" without the percentage?

benjaminrich commented 1 year ago

This is entirely reasonable. I try to avoid having too many options for special cases though to keep the interface manageable. You could always do something like this:

library(table1)

dat <- data.frame(x=factor(sample(LETTERS[1:4], 20, replace=T)))
dat <- dat[dat$x != "C",, drop=F]

special0handling <- function(x, ...) {
    gsub("0 (0%)", "0", render.default(x, ...), fixed=TRUE)
}

table1(~ x, dat, render.categorical=special0handling)
billdenney commented 1 year ago

I fully support not having too many options! (I share that opinion.) And, thanks for the method to handle it without changes.

As one last ask on the topic: Since "0" and "0%" are duplicate information, does it seem reasonable to change the default to just show "0"? (If not, no worries, and I'll use the suggested change.)

benjaminrich commented 1 year ago

Like I said, I think it's entirely reasonable to just use "0" in these situation, but both approaches have their disadvantages in terms of consistency and aesthetics, in my opinion. Therefore, my personal opinion is that the suggestion doesn't provide enough value to warrant changing the default at this time. (In fact, at one point early on I did consider making "0" the default, but I decided against it in the end.) But, I'll admit that I do continue to think about exactly this issue.

billdenney commented 1 year ago

All good. Thanks for the consideration.