rstatgtsummary

rounding to nearst 0 or 5 in gtsummary outputbale


I need help in rounding the last digit to 0 or 5 in my gtsummary output table. any help is much appreciated

library(tidyverse)
library(gtsummary)

data %>% 
tbl_summary(
  by = hba1c,
  include = -id
) 

here is data

structure(list(id = 1:100, age = c("a", "a", "b", "a", "b", "b", 
"b", "c", "c", "c", "a", "a", "b", "a", "b", "b", "b", "c", "c", 
"c", "a", "a", "b", "a", "b", "b", "b", "c", "c", "c", "a", "a", 
"b", "a", "b", "b", "b", "c", "c", "c", "a", "a", "b", "a", "b", 
"b", "b", "c", "c", "c", "a", "a", "b", "a", "b", "b", "b", "c", 
"c", "c", "a", "a", "b", "a", "b", "b", "b", "c", "c", "c", "a", 
"a", "b", "a", "b", "b", "b", "c", "c", "c", "a", "a", "b", "a", 
"b", "b", "b", "c", "c", "c", "a", "a", "b", "a", "b", "b", "b", 
"c", "c", "c"), sex = c("M", "F", "M", "M", "M", "F", "F", "F", 
"F", "M", "F", "M", "M", "M", "F", "F", "F", "F", "M", "F", "M", 
"M", "M", "F", "F", "F", "F", "M", "F", "M", "M", "M", "F", "F", 
"F", "F", "M", "F", "M", "M", "M", "F", "F", "F", "F", "M", "F", 
"M", "M", "M", "F", "F", "F", "F", "M", "F", "M", "M", "M", "F", 
"F", "F", "F", "M", "F", "M", "M", "M", "F", "F", "F", "F", "M", 
"M", "F", "F", "F", "F", "M", "F", "M", "M", "M", "F", "F", "F", 
"F", "M", "M", "F", "F", "F", "F", "M", "F", "M", "M", "M", "F", 
"F"), country = c("eng", "eng", "eng", "eng", "wale", "wale", 
"wale", "scot", "scot", "scot", "eng", "eng", "eng", "eng", "wale", 
"wale", "wale", "scot", "scot", "scot", "eng", "eng", "eng", 
"eng", "wale", "wale", "wale", "scot", "scot", "scot", "eng", 
"eng", "eng", "eng", "wale", "wale", "wale", "scot", "scot", 
"scot", "eng", "eng", "eng", "eng", "wale", "wale", "wale", "scot", 
"scot", "scot", "eng", "eng", "eng", "eng", "wale", "wale", "wale", 
"scot", "scot", "scot", "eng", "eng", "eng", "eng", "wale", "wale", 
"wale", "scot", "scot", "scot", "eng", "eng", "eng", "eng", "wale", 
"wale", "wale", "scot", "scot", "scot", "eng", "eng", "eng", 
"eng", "wale", "wale", "wale", "scot", "scot", "scot", "eng", 
"eng", "eng", "eng", "wale", "wale", "wale", "scot", "scot", 
"scot"), edu = c("x", "x", "x", "y", "y", "y", "z", "z", "z", 
"z", "x", "x", "x", "y", "y", "y", "z", "z", "z", "z", "x", "x", 
"x", "y", "y", "y", "z", "z", "z", "z", "x", "x", "x", "y", "y", 
"y", "z", "z", "z", "z", "x", "x", "x", "y", "y", "y", "z", "z", 
"z", "z", "x", "x", "x", "y", "y", "y", "z", "z", "z", "z", "x", 
"x", "x", "y", "y", "y", "z", "z", "z", "z", "x", "x", "x", "y", 
"y", "y", "z", "z", "z", "z", "x", "x", "x", "y", "y", "y", "z", 
"z", "z", "z", "x", "x", "x", "y", "y", "y", "z", "z", "z", "z"
), hba1c = c(1L, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, 1L, 1L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, NA, NA, NA, NA, NA, 
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 
NA, NA, NA, NA, NA, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-100L))

Solution

  • You can create your own styling function to do this.

    round_05 <- function(x) {
      ceiling(x/5) * 5
    } 
    

    And then use it in your tbl_summary call to round the frequencies while keeping the percentages as is. If you want to round the percentages too, then create another similar function that multiplies x by 100 first and replace the "0" with this function.

    df %>% 
      tbl_summary(
        by = hba1c,
        include = -id,
        digits=list(everything() ~ c(round_05, 0))
        # digits=list(everything() ~ c(round_05, round_pct))
      )
    

    enter image description here