How to modify the default variable type defined by "all_categorical()" in "gtsummary"? when mean of ordinal variable were wanted?

Viewed 85

The variable "Var2" has been set as categorical variable by default, while the mean(sd) were needed sometimes. So I am interested in how to modified this.

data_table_1 = 
  data %>% 
  dplyr::select(group, var1, var2)


data_table_1 %>%
  tbl_summary(by = group, missing = "no",
              statistic = list(all_continuous() ~ "{mean} ± {sd}",
                               all_categorical() ~ "{n} ({p}%)"),
              digits = list(all_continuous() ~ c(2, 2))) %>%
  add_p(test = list(all_continuous() ~ "pttest2", all_categorical() ~ "pttest2"),
        pvalue_fun = function(x) sprintf(x, fmt='%#.3f')) 

enter image description here

1 Answers

The function tbl_summary() does its best to guess the type of summary that best suits the data...but this is not always how you'd like to summarize your data. To update the default summary type, use the type= argument. In this case you'd want to include type = list(Var2 ~ "continuous") to summarize the data continuously.

Hope this helps!

Related