table1 icon indicating copy to clipboard operation
table1 copied to clipboard

Specifying significant numbers for percent

Open slevu opened this issue 2 years ago • 2 comments

I am looking for a way to express counts and percentage with k significant numbers, i.e. overcoming the current default of setting the number of decimal for the percents. In my exemple, I would like 91% and 9.1%, but with combination of parameters digits and digits.pct, I can only have one of the two. Thanks x <- c(rep("A", 10), "B") table1( ~ x, digits.pct = 1)

slevu avatar Mar 07 '23 12:03 slevu

Okay, I admit this isn't great, but the following works:

table1( ~ x, render.categorical = function(x, ...) {
    c("", sapply(stats.default(x), function(y) with(y, sprintf("%s (%0.2g%%)", FREQ, PCT))))
})

I need to think about a better way to integrate the functionality.

benjaminrich avatar Mar 08 '23 23:03 benjaminrich

Great. Thanks Benjamin. I get that the point is to avoid using stats.apply.rounding() in the definition of render.categorical. I adapted your solution for my needs with signif_pad().

slevu avatar Mar 09 '23 11:03 slevu