Problems with add_difference() in gtsummary

Hi, I have this df

df
# A tibble: 248 × 2
   asignado     mxsitam
   <chr>        <chr>  
 1 Control      No     
 2 Control      No     
 3 Intervencion No     
 4 Intervencion Si     
 5 Intervencion Si     
 6 Intervencion Si     
 7 Control      No     
 8 Intervencion Si     
 9 Control      Si     
10 Control      Si     
# ℹ 238 more rows

I want to make a table that includes a column showing the difference between percentages. The problem is that it does not calculate it for the values of the variables but for the variables themselves, and I do not understand what calculation it does.
This is the code.

df %>%
  mutate_all(as.factor) %>%
  tbl_summary(by= "asignado",
              missing = "always",
              digits = list(all_categorical() ~ c(0,1)),
              statistic = list(all_categorical() ~ "{n} ({p})"),
              missing_text= "Casos perdidos",
              percent= "column") %>% 
  add_overall() %>%
  modify_header(label = "") %>%
  add_difference()

This is the output

What I want is the difference between the Control and Intervention percentages, for example 73.6 - 66.7.

The strange thing is that with this code I have something more similar, but it does not show me for all levels and doesn't show me the CI.

df %>%
    mutate(mxsitam= as.integer(if_else(mxsitam== "No", 0,1))) %>% 
    tbl_summary(by= "asignado",
                missing = "always",
                digits = list(all_categorical() ~ c(0,1)),
                statistic = list(all_categorical() ~ "{n} ({p})"),
                missing_text= "Casos perdidos",
                percent= "column") %>% 
    add_overall() %>%
    modify_header(label = "") %>%
    add_difference()

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.