tbl_cross from gtsummary: Modify decimals of statistic and df separately

Viewed 37

First question in stackoverflow, sorry if there are inaccuracies.

I need the possibility to separately modify the number of decimals of the statistic and df added with modify_fmt_fun. Specifically, I need the statistic X^2 to have 2 decimals and the df 0 decimals.

The next code makes the table reproducible:

library(gtsummary)
library(tidyverse)

# Dummy database from fruequencies
sex_smoking_table <- 
  c(18, 42, 7, 6, 69, 58) %>% 
  array(dim = c(2,3))

dimnames(sex_smoking_table) = 
  list(sex = c('male', 'female'),
       smoking = c("smokers", "ex_smokers", "non_smokers"))

sex_smoking <-  sex_smoking_table %>% 
  as.data.frame.table() %>% 
  rowwise() %>% 
  do(data.frame(rep(.$sex, .$Freq), .$smoking)) %>% 
  rename("Sex" = contains("sex"), "Smoking" = contains("smoking")) 

# Cross table
tbl_sex_smoking <- sex_smoking %>% 
  tbl_cross(
    row = Sex,
    col = Smoking
  ) %>% 
  add_p() %>%
  modify_header(
    statistic = "**X^2**", parameter = "**df**"
  ) %>%
  modify_fmt_fun(
    c(statistic, parameter) ~ purrr::partial(style_sigfig, digits = 3)
  )
tbl_sex_smoking

enter image description here

0 Answers
Related